forked from mcasperson/OctopusRecommendationEngine
-
Notifications
You must be signed in to change notification settings - Fork 7
Add check for SHA-1 certificates #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
688ccd2
Create new check for SHA1-certificates
harrisonmeister cc2a4f6
Add missing machine policy
harrisonmeister 19ff02a
Clean-up naming convention and fix terraform
harrisonmeister 50d84fe
Fix Id of Check
harrisonmeister 14eedf2
Fix test, checks should pass as no sha1 certs
harrisonmeister 4d0f64f
Fix spelling in duplicated_git_creds_check [skip ci]
harrisonmeister File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
188 changes: 188 additions & 0 deletions
188
internal/checks/security/octopus_sha1_certificates_check.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,188 @@ | ||
| package security | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "errors" | ||
| "fmt" | ||
| "net/http" | ||
| "sort" | ||
| "strings" | ||
|
|
||
| "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/client" | ||
| "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/machines" | ||
| "github.com/OctopusSolutionsEngineering/OctopusRecommendationEngine/internal/checks" | ||
| "github.com/OctopusSolutionsEngineering/OctopusRecommendationEngine/internal/client_wrapper" | ||
| "github.com/OctopusSolutionsEngineering/OctopusRecommendationEngine/internal/config" | ||
| "go.uber.org/zap" | ||
| ) | ||
|
|
||
| const ( | ||
| OctoLintSha1Certificates = "OctoLintSha1Certificates" | ||
| sha1Alg = "sha1RSA" | ||
| ) | ||
|
|
||
| // OctopusSha1CertificatesCheck checks to see if any targets, workers or the server itself is using a sha1 certificate | ||
| type OctopusSha1CertificatesCheck struct { | ||
| client *client.Client | ||
| errorHandler checks.OctopusClientErrorHandler | ||
| config *config.OctolintConfig | ||
| } | ||
|
|
||
| type Sha1CertificateResult struct { | ||
| Name string | ||
| Type string // "Target", "Worker", or "Global" | ||
| } | ||
|
|
||
| type ServerCertificate struct { | ||
| ID string `json:"Id"` | ||
| Name string `json:"Name"` | ||
| Thumbprint string `json:"Thumbprint"` | ||
| SignatureAlgorithm string `json:"SignatureAlgorithm"` | ||
| Links map[string]string `json:"Links"` | ||
| } | ||
|
|
||
| func NewOctopusSha1CertificatesCheck(client *client.Client, config *config.OctolintConfig, errorHandler checks.OctopusClientErrorHandler) OctopusSha1CertificatesCheck { | ||
| return OctopusSha1CertificatesCheck{config: config, client: client, errorHandler: errorHandler} | ||
| } | ||
|
|
||
| func (o OctopusSha1CertificatesCheck) Id() string { | ||
| return OctoLintSha1Certificates | ||
| } | ||
|
|
||
| // fetchServerCertificate gets the server certificate object and returns it. | ||
| func fetchServerCertificate(url, apiKey, accessToken string) (*ServerCertificate, error) { | ||
| requestURL := fmt.Sprintf("%s/api/configuration/certificates/certificate-global", url) | ||
|
|
||
| req, err := http.NewRequest(http.MethodGet, requestURL, nil) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| if apiKey != "" { | ||
| req.Header.Set("X-Octopus-ApiKey", apiKey) | ||
| } else if accessToken != "" { | ||
| req.Header.Set("Authorization", "Bearer "+accessToken) | ||
| } | ||
|
|
||
| res, err := http.DefaultClient.Do(req) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| defer res.Body.Close() | ||
|
|
||
| if res.StatusCode != http.StatusOK { | ||
| return nil, fmt.Errorf("unexpected status code: %d", res.StatusCode) | ||
| } | ||
|
|
||
| var cert ServerCertificate | ||
| if err := json.NewDecoder(res.Body).Decode(&cert); err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return &cert, nil | ||
| } | ||
|
|
||
| // hasSha1Certificate checks if an endpoint has CertificateSignatureAlgorithm == sha1Alg | ||
| func hasSha1Certificate(ep machines.IEndpoint) bool { | ||
| if ep == nil { | ||
| return false | ||
| } | ||
|
|
||
| switch e := ep.(type) { | ||
| case *machines.ListeningTentacleEndpoint: | ||
| return e.CertificateSignatureAlgorithm == sha1Alg | ||
| case *machines.PollingTentacleEndpoint: | ||
| return e.CertificateSignatureAlgorithm == sha1Alg | ||
| default: | ||
| return false | ||
| } | ||
| } | ||
|
|
||
| // addSha1FromMachines is a top-level generic helper (function literals can't have type params). | ||
| func addSha1FromMachines[T any]( | ||
| results *[]Sha1CertificateResult, | ||
| items []T, | ||
| getName func(T) string, | ||
| getEndpoint func(T) machines.IEndpoint, | ||
| typ string, | ||
| ) { | ||
| for _, item := range items { | ||
| if hasSha1Certificate(getEndpoint(item)) { | ||
| *results = append(*results, Sha1CertificateResult{Name: getName(item), Type: typ}) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func (o OctopusSha1CertificatesCheck) Execute(concurrency int) (checks.OctopusCheckResult, error) { | ||
| if o.client == nil { | ||
| return nil, errors.New("octoclient is nil") | ||
| } | ||
|
|
||
| zap.L().Debug("Starting check " + o.Id()) | ||
| defer func() { | ||
| zap.L().Debug("Ended check " + o.Id()) | ||
| }() | ||
|
|
||
| var results []Sha1CertificateResult | ||
|
|
||
| // Check server certificate | ||
| cert, err := fetchServerCertificate(o.config.Url, o.config.ApiKey, o.config.AccessToken) | ||
| if err != nil { | ||
| return o.errorHandler.HandleError(o.Id(), checks.Security, err) | ||
| } | ||
| if cert != nil && cert.SignatureAlgorithm == sha1Alg { | ||
| results = append(results, Sha1CertificateResult{Name: cert.Name, Type: "Global"}) | ||
| } | ||
|
|
||
| // Check deployment targets | ||
| targets, err := client_wrapper.GetMachines(o.config.MaxSha1CertificatesMachines, o.client, o.client.GetSpaceID()) | ||
| if err != nil { | ||
| return o.errorHandler.HandleError(o.Id(), checks.Security, err) | ||
| } | ||
| addSha1FromMachines(&results, targets, | ||
| func(m *machines.DeploymentTarget) string { return m.Name }, | ||
| func(m *machines.DeploymentTarget) machines.IEndpoint { return m.Endpoint }, | ||
| "Target", | ||
| ) | ||
|
|
||
| // Check workers | ||
| workers, err := client_wrapper.GetWorkers(o.config.MaxSha1CertificatesMachines, o.client, o.client.GetSpaceID()) | ||
| if err != nil { | ||
| return o.errorHandler.HandleError(o.Id(), checks.Security, err) | ||
| } | ||
| addSha1FromMachines(&results, workers, | ||
| func(w *machines.Worker) string { return w.Name }, | ||
| func(w *machines.Worker) machines.IEndpoint { return w.Endpoint }, | ||
| "Worker", | ||
| ) | ||
|
|
||
| // Provide results | ||
| if len(results) > 0 { | ||
| // Sort by Type then Name for stable output | ||
| sort.Slice(results, func(i, j int) bool { | ||
| if results[i].Type == results[j].Type { | ||
| return results[i].Name < results[j].Name | ||
| } | ||
| return results[i].Type < results[j].Type | ||
| }) | ||
|
|
||
| lines := make([]string, len(results)) | ||
| for i, m := range results { | ||
| lines[i] = fmt.Sprintf("%s: %s", m.Type, m.Name) | ||
| } | ||
|
|
||
| return checks.NewOctopusCheckResultImpl( | ||
| "The following resources use a SHA1 certificate:\n"+strings.Join(lines, "\n"), | ||
| o.Id(), | ||
| "", | ||
| checks.Warning, | ||
| checks.Security), nil | ||
| } | ||
|
|
||
| return checks.NewOctopusCheckResultImpl( | ||
| "There are no uses of SHA1 certificates in targets, workers or the main Server Certificate", | ||
| o.Id(), | ||
| "", | ||
| checks.Ok, | ||
| checks.Security), nil | ||
| } |
61 changes: 61 additions & 0 deletions
61
internal/checks/security/octopus_sha1_certificates_check_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| package security | ||
|
|
||
| import ( | ||
| "errors" | ||
| "path/filepath" | ||
| "testing" | ||
|
|
||
| "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/client" | ||
| "github.com/OctopusSolutionsEngineering/OctopusRecommendationEngine/internal/checks" | ||
| "github.com/OctopusSolutionsEngineering/OctopusRecommendationEngine/internal/config" | ||
| "github.com/OctopusSolutionsEngineering/OctopusTerraformTestFramework/octoclient" | ||
| "github.com/OctopusSolutionsEngineering/OctopusTerraformTestFramework/test" | ||
| ) | ||
|
|
||
| func TestSha1Certificates(t *testing.T) { | ||
| testFramework := test.OctopusContainerTest{} | ||
|
|
||
| testFramework.ArrangeTest(t, func(t *testing.T, container *test.OctopusContainer, client *client.Client) error { | ||
| // Act: Deploy Terraform scenario that sets up SHA1 certificates | ||
| newSpaceId, err := testFramework.Act( | ||
| t, | ||
| container, | ||
| filepath.Join("..", "..", "..", "test", "terraform"), | ||
| "33-sha1certificates", // folder containing your Terraform scenario | ||
| []string{}, | ||
| ) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| // Create a client for the new space | ||
| newSpaceClient, err := octoclient.CreateClient(container.URI, newSpaceId, test.ApiKey) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| // Create the check | ||
| check := NewOctopusSha1CertificatesCheck( | ||
| newSpaceClient, | ||
| &config.OctolintConfig{ | ||
| Url: container.URI, | ||
| ApiKey: test.ApiKey, | ||
| MaxSha1CertificatesMachines: 100, | ||
| }, | ||
| checks.OctopusClientPermissiveErrorHandler{}, | ||
| ) | ||
|
|
||
| // Execute the check | ||
| result, err := check.Execute(2) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| // Assert | ||
| if result == nil || result.Severity() != checks.Ok { | ||
| return errors.New("check should have passed") | ||
| } | ||
|
|
||
| return nil | ||
| }) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| package client_wrapper | ||
|
|
||
| import ( | ||
| "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/machines" | ||
| "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/newclient" | ||
| "github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/workers" | ||
| ) | ||
|
|
||
| func GetWorkers(limit int, client newclient.Client, spaceID string) ([]*machines.Worker, error) { | ||
| if limit == 0 { | ||
| return workers.GetAll(client, spaceID) | ||
| } | ||
|
|
||
| result, err := workers.Get(client, spaceID, machines.WorkersQuery{ | ||
| Take: limit, | ||
| }) | ||
|
|
||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return result.Items, nil | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| terraform { | ||
| required_providers { | ||
| octopusdeploy = { source = "OctopusDeployLabs/octopusdeploy", version = "0.30.4" } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| resource "octopusdeploy_environment" "development_environment" { | ||
| allow_dynamic_infrastructure = true | ||
| description = "A development environment" | ||
| name = "Development" | ||
| use_guided_failure = false | ||
| } | ||
|
|
||
| resource "octopusdeploy_environment" "test_environment" { | ||
| allow_dynamic_infrastructure = true | ||
| description = "A test environment" | ||
| name = "Test" | ||
| use_guided_failure = false | ||
| } | ||
|
|
||
| resource "octopusdeploy_environment" "production_environment" { | ||
| allow_dynamic_infrastructure = true | ||
| description = "A production environment" | ||
| name = "Production" | ||
| use_guided_failure = false | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😆