Some checks failed
Server CI / Postgres (shard 0) (push) Has been skipped
Server CI / Postgres (shard 3) (push) Has been skipped
BuildEnv Docker Image / build-image (push) Failing after 4s
BuildEnv Docker Image / build-image-fips (push) Failing after 4s
Migration Review & Release Notes / Generate migration review & release notes (push) Failing after 3s
Server CI / Build Environment (push) Failing after 3s
Server CI / Postgres (shard 1) (push) Has been skipped
Tools CI / check-style (mattermost-govet) (push) Failing after 4s
Server CI / Postgres (shard 2) (push) Has been skipped
Tools CI / check-style (mmgotool) (push) Failing after 3s
Server CI / Elasticsearch v8 Compatibility (push) Has been skipped
Server CI / OpenSearch v2 Compatibility (push) Has been skipped
Server CI / Postgres FIPS (shard 0) (push) Has been skipped
Tools CI / Test (mattermost-govet) (push) Failing after 3s
Server CI / Postgres FIPS (shard 1) (push) Has been skipped
Server CI / Postgres FIPS (shard 2) (push) Has been skipped
Tools CI / Test (mmgotool) (push) Failing after 3s
Server CI / Postgres FIPS (shard 3) (push) Has been skipped
Server CI / Run mmctl tests (push) Has been skipped
Server CI / Run mmctl tests (FIPS) (push) Has been skipped
Server CI / Merge Postgres Test Results (push) Has been skipped
Server CI / Merge Postgres FIPS Test Results (push) Has been skipped
Server CI / Check backport migrations (push) Has been skipped
Server CI / Check generated files (push) Has been skipped
Server CI / check-style (push) Has been skipped
Server CI / Vet API (push) Has been skipped
Server CI / Build mattermost server app (push) Has been skipped
Server CI / Server CI Complete (push) Failing after 2s
Server CI / Server CI Artifacts (push) Has been skipped
Server CI / Sentry Upload (push) Has been skipped
Server CI / Server CI Report (push) Failing after 5m1s
Server CI Nightly Race / Race Detector (push) Has been skipped
CodeQL / Analyze (go) (push) Has been cancelled
CodeQL / Analyze (javascript) (push) Has been cancelled
Webapp CI Cache Warm / Warm Webapp node_modules Cache (push) Has been cancelled
E2E CI Cache Warm / Warm E2E npm Registry Cache (push) Has been cancelled
API / build (push) Has been cancelled
Web App CI / check-types (push) Has been cancelled
Web App CI / check-lint (push) Has been cancelled
Web App CI / check-i18n (push) Has been cancelled
Web App CI / check-external-links (push) Has been cancelled
Web App CI / test (platform) (push) Has been cancelled
Web App CI / test (mattermost-redux) (push) Has been cancelled
Web App CI / test (channels shard 1/4) (push) Has been cancelled
Web App CI / test (channels shard 2/4) (push) Has been cancelled
Web App CI / test (channels shard 3/4) (push) Has been cancelled
Web App CI / test (channels shard 4/4) (push) Has been cancelled
Web App CI / upload-coverage (push) Has been cancelled
Web App CI / build (push) Has been cancelled
YAML Lint / yamllint (push) Has been cancelled
Push mirrored docker images / cd/Push mirrored docker images (push) Has been cancelled
docs-cd / Build and deploy docs (push) Has been cancelled
Docs CI / build (push) Has been cancelled
E2E Cache Warm: Cypress deps / Warm Cypress deps cache (push) Has been cancelled
Scorecards supply-chain security / Scorecard analysis (push) Has been cancelled
Server CI Weekly / Postgres FIPS (push) Has been skipped
Server CI Weekly / Run mmctl tests (FIPS) (push) Has been skipped
Server CI Weekly / Postgres with binary parameters (push) Has been skipped
Server CI Weekly / Build Environment (push) Failing after 1s
Server CI Cache Warm / Warm Go Module and Build Cache (push) Failing after 2s
Server CI Nightly Race / Build Environment (push) Failing after 1s
195 lines
4.8 KiB
Go
195 lines
4.8 KiB
Go
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
// See LICENSE.txt for license information.
|
|
|
|
package commands
|
|
|
|
import (
|
|
"context"
|
|
"crypto/x509"
|
|
"encoding/json"
|
|
"net"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/gorilla/mux"
|
|
"github.com/mattermost/mattermost/server/public/model"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestCheckVersionMatch(t *testing.T) {
|
|
testCases := []struct {
|
|
Name string
|
|
Version string
|
|
ServerVersion string
|
|
Expected bool
|
|
ErrExpected bool
|
|
}{
|
|
{
|
|
Name: "Both versions are equal",
|
|
Version: "1.2.3",
|
|
ServerVersion: "1.2.3.dev.993e5a2cb546b0116ecaae1862b6a1c6.true",
|
|
Expected: true,
|
|
},
|
|
{
|
|
Name: "Only patch version is different",
|
|
Version: "1.2.3",
|
|
ServerVersion: "1.2.7.dev.993e5a2cb546b0116ecaae1862b6a1c6.false",
|
|
Expected: true,
|
|
},
|
|
{
|
|
Name: "Major version is greater",
|
|
Version: "1.2.3",
|
|
ServerVersion: "7.0.0.7.0.0.8215d92df0b8458789408eb07ccfdaae.false",
|
|
Expected: false,
|
|
},
|
|
{
|
|
Name: "Major version is less",
|
|
Version: "8.2.3",
|
|
ServerVersion: "7.0.0.7.0.0.8215d92df0b8458789408eb07ccfdaae.false",
|
|
Expected: false,
|
|
},
|
|
{
|
|
Name: "Minor version is greater",
|
|
Version: "1.2.3",
|
|
ServerVersion: "1.3.3.1.3.3.8215d92df0b8458789408eb07ccfdaae.true",
|
|
Expected: true,
|
|
},
|
|
{
|
|
Name: "Minor version is less",
|
|
Version: "1.2.3",
|
|
ServerVersion: "1.1.3.1.1.3.8215d92df0b8458789408eb07ccfdaae.false",
|
|
Expected: false,
|
|
},
|
|
{
|
|
Name: "Both versions are equal but one has v in front of it",
|
|
Version: "v1.2.3",
|
|
ServerVersion: "1.2.3.dev.8215d92df0b8458789408eb07ccfdaae.false",
|
|
Expected: true,
|
|
},
|
|
{
|
|
Name: "unspecified version",
|
|
Version: "",
|
|
ServerVersion: "1.2.3",
|
|
Expected: false,
|
|
ErrExpected: true,
|
|
},
|
|
{
|
|
Name: "bad version",
|
|
Version: "1.2.3",
|
|
ServerVersion: "1.2",
|
|
Expected: false,
|
|
ErrExpected: true,
|
|
},
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
t.Run(tc.Name, func(t *testing.T) {
|
|
res, err := CheckVersionMatch(tc.Version, tc.ServerVersion)
|
|
require.True(t, (err != nil) == tc.ErrExpected)
|
|
require.Equal(t, tc.Expected, res)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestVerifyCertificates(t *testing.T) {
|
|
testCases := []struct {
|
|
Name string
|
|
Chains [][]*x509.Certificate
|
|
ExpectedError bool
|
|
}{
|
|
{
|
|
Name: "One chain with a root SHA1 cert",
|
|
Chains: [][]*x509.Certificate{
|
|
{
|
|
{SignatureAlgorithm: x509.SHA256WithRSA},
|
|
{SignatureAlgorithm: x509.SHA1WithRSA},
|
|
},
|
|
},
|
|
ExpectedError: false,
|
|
},
|
|
{
|
|
Name: "One chain with an intermediate SHA1 cert",
|
|
Chains: [][]*x509.Certificate{
|
|
{
|
|
{SignatureAlgorithm: x509.SHA256WithRSA},
|
|
{SignatureAlgorithm: x509.SHA1WithRSA},
|
|
{SignatureAlgorithm: x509.SHA1WithRSA},
|
|
},
|
|
},
|
|
ExpectedError: true,
|
|
},
|
|
{
|
|
Name: "One valid chain and other invalid",
|
|
Chains: [][]*x509.Certificate{
|
|
{
|
|
{SignatureAlgorithm: x509.SHA256WithRSA},
|
|
{SignatureAlgorithm: x509.SHA1WithRSA},
|
|
{SignatureAlgorithm: x509.SHA1WithRSA},
|
|
},
|
|
{
|
|
{SignatureAlgorithm: x509.SHA256WithRSA},
|
|
{SignatureAlgorithm: x509.SHA256WithRSA},
|
|
{SignatureAlgorithm: x509.SHA1WithRSA},
|
|
},
|
|
},
|
|
ExpectedError: false,
|
|
},
|
|
{
|
|
Name: "Two invalid chains",
|
|
Chains: [][]*x509.Certificate{
|
|
{
|
|
{SignatureAlgorithm: x509.SHA256WithRSA},
|
|
{SignatureAlgorithm: x509.SHA1WithRSA},
|
|
{SignatureAlgorithm: x509.SHA1WithRSA},
|
|
},
|
|
{
|
|
{SignatureAlgorithm: x509.SHA256WithRSA},
|
|
{SignatureAlgorithm: x509.DSAWithSHA1},
|
|
{SignatureAlgorithm: x509.DSAWithSHA1},
|
|
},
|
|
},
|
|
ExpectedError: true,
|
|
},
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
t.Run(tc.Name, func(t *testing.T) {
|
|
err := VerifyCertificates([][]byte{}, tc.Chains)
|
|
if tc.ExpectedError {
|
|
require.Error(t, err)
|
|
} else {
|
|
require.NoError(t, err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestNewAPIv4Client(t *testing.T) {
|
|
t.Run("should take http proxy into account", func(t *testing.T) {
|
|
router := mux.NewRouter()
|
|
router.Handle("/api/v4/users/me", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
user := &model.User{
|
|
Id: model.NewId(),
|
|
}
|
|
err := json.NewEncoder(w).Encode(user)
|
|
require.NoError(t, err)
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
}))
|
|
s := httptest.NewServer(router)
|
|
defer s.Close()
|
|
|
|
proxyAddr := s.Listener.Addr().String()
|
|
_, port, err := net.SplitHostPort(proxyAddr)
|
|
require.NoError(t, err)
|
|
|
|
// t.Setenv prevents t.Parallel — env var has no config equivalent
|
|
t.Setenv("HTTP_PROXY", proxyAddr)
|
|
|
|
client := NewAPIv4Client("http://somethingelse:"+port, false, false)
|
|
_, _, err = client.GetMe(context.Background(), "")
|
|
require.NoError(t, err)
|
|
})
|
|
}
|