-
Notifications
You must be signed in to change notification settings - Fork 20
Add gravitational_trace.nocrypto build tag #119
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
4 commits
Select commit
Hold shift + click to select a range
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
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,26 @@ | ||
| //go:build !gravitational_trace.nocrypto | ||
|
|
||
| // Copyright 2026 Gravitational, Inc. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package trace | ||
|
|
||
| import "crypto/x509" | ||
|
|
||
| // these type aliases are used by [ConvertSystemError]; builds with the nocrypto | ||
| // tag will define them differently | ||
| type ( | ||
| x509SystemRootsError = x509.SystemRootsError | ||
| x509UnknownAuthorityError = x509.UnknownAuthorityError | ||
| ) |
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,35 @@ | ||
| //go:build gravitational_trace.nocrypto | ||
|
codingllama marked this conversation as resolved.
|
||
|
|
||
| // Copyright 2026 Gravitational, Inc. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package trace | ||
|
|
||
| // these unexported error types are never constructed but they are referenced by | ||
| // [ConvertSystemError]; in builds without the nocrypto tag they are aliases of | ||
| // errors in [crypto/x509] | ||
| type ( | ||
| x509SystemRootsError struct{} | ||
| x509UnknownAuthorityError struct{} | ||
| ) | ||
|
|
||
| // Error implements [error]. | ||
| func (x509SystemRootsError) Error() string { | ||
| return "trace.x509SystemRootsError (this is a bug)" | ||
| } | ||
|
|
||
| // Error implements [error]. | ||
| func (x509UnknownAuthorityError) Error() string { | ||
| return "trace.x509UnknownAuthorityError (this is a bug)" | ||
| } | ||
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 |
|---|---|---|
| @@ -1,3 +1,5 @@ | ||
| //go:build !gravitational_trace.nocrypto | ||
|
|
||
| /* | ||
| Copyright 2020 Gravitational, Inc. | ||
|
|
||
|
|
||
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,214 @@ | ||
| //go:build !gravitational_trace.nocrypto | ||
|
|
||
| // Copyright 2026 Gravitational, Inc. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package trace | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "net/http" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestGetFields(t *testing.T) { | ||
| testErr := fmt.Errorf("description") | ||
| assert.Equal(t, map[string]interface{}{}, GetFields(testErr)) | ||
|
|
||
| fields := map[string]interface{}{ | ||
| "test_key": "test_value", | ||
| } | ||
| err := WithFields(Wrap(testErr), fields) | ||
| assert.Equal(t, fields, GetFields(err)) | ||
|
|
||
| // ensure that you can get fields from a proxyError | ||
| e := roundtripError(err) | ||
| assert.Equal(t, fields, GetFields(e)) | ||
| } | ||
|
|
||
| func roundtripError(err error) error { | ||
| w := newTestWriter() | ||
| WriteError(w, err) | ||
|
|
||
| outErr := ReadError(w.StatusCode, w.Body) | ||
| return outErr | ||
| } | ||
|
|
||
| func TestGenericErrors(t *testing.T) { | ||
| testCases := []struct { | ||
| Err Error | ||
| Predicate func(error) bool | ||
| StatusCode int | ||
| comment string | ||
| }{ | ||
| { | ||
| Err: NotFound("not found"), | ||
| Predicate: IsNotFound, | ||
| StatusCode: http.StatusNotFound, | ||
| comment: "not found error", | ||
| }, | ||
| { | ||
| Err: AlreadyExists("already exists"), | ||
| Predicate: IsAlreadyExists, | ||
| StatusCode: http.StatusConflict, | ||
| comment: "already exists error", | ||
| }, | ||
| { | ||
| Err: BadParameter("is bad"), | ||
| Predicate: IsBadParameter, | ||
| StatusCode: http.StatusBadRequest, | ||
| comment: "bad parameter error", | ||
| }, | ||
| { | ||
| Err: CompareFailed("is bad"), | ||
| Predicate: IsCompareFailed, | ||
| StatusCode: http.StatusPreconditionFailed, | ||
| comment: "comparison failed error", | ||
| }, | ||
| { | ||
| Err: AccessDenied("denied"), | ||
| Predicate: IsAccessDenied, | ||
| StatusCode: http.StatusForbidden, | ||
| comment: "access denied error", | ||
| }, | ||
| { | ||
| Err: ConnectionProblem(nil, "prob"), | ||
| Predicate: IsConnectionProblem, | ||
| StatusCode: http.StatusRequestTimeout, | ||
| comment: "connection error", | ||
| }, | ||
| { | ||
| Err: LimitExceeded("limit exceeded"), | ||
| Predicate: IsLimitExceeded, | ||
| StatusCode: http.StatusTooManyRequests, | ||
| comment: "limit exceeded error", | ||
| }, | ||
| { | ||
| Err: NotImplemented("not implemented"), | ||
| Predicate: IsNotImplemented, | ||
| StatusCode: http.StatusNotImplemented, | ||
| comment: "not implemented error", | ||
| }, | ||
| } | ||
|
|
||
| for _, testCase := range testCases { | ||
| SetDebug(true) | ||
| err := testCase.Err | ||
|
|
||
| var traceErr *TraceErr | ||
| var ok bool | ||
| if traceErr, ok = err.(*TraceErr); !ok { | ||
| t.Fatalf("Expected error to be of type *TraceErr: %#v", err) | ||
| } | ||
|
|
||
| assert.NotEmpty(t, traceErr.Traces, testCase.comment) | ||
| assert.Regexp(t, ".*.trace_crypto_test\\.go.*", line(DebugReport(err)), testCase.comment) | ||
| assert.NotRegexp(t, ".*.errors\\.go.*", line(DebugReport(err)), testCase.comment) | ||
| assert.NotRegexp(t, ".*.trace\\.go.*", line(DebugReport(err)), testCase.comment) | ||
| assert.True(t, testCase.Predicate(err), testCase.comment) | ||
|
|
||
| w := newTestWriter() | ||
| WriteError(w, err) | ||
|
|
||
| outErr := ReadError(w.StatusCode, w.Body) | ||
| if _, ok := outErr.(proxyError); !ok { | ||
| t.Fatalf("Expected error to be of type proxyError: %#v", outErr) | ||
| } | ||
| assert.True(t, testCase.Predicate(outErr), testCase.comment) | ||
|
|
||
| SetDebug(false) | ||
| w = newTestWriter() | ||
| WriteError(w, err) | ||
| outErr = ReadError(w.StatusCode, w.Body) | ||
| assert.True(t, testCase.Predicate(outErr), testCase.comment) | ||
| } | ||
| } | ||
|
|
||
| // Make sure we write some output produced by standard errors | ||
|
codingllama marked this conversation as resolved.
|
||
| func TestWriteExternalErrors(t *testing.T) { | ||
| err := Wrap(fmt.Errorf("snap!")) | ||
|
|
||
| SetDebug(true) | ||
| w := newTestWriter() | ||
| WriteError(w, err) | ||
| extErr := ReadError(w.StatusCode, w.Body) | ||
| assert.Equal(t, http.StatusInternalServerError, w.StatusCode) | ||
| assert.Regexp(t, ".*.snap.*", strings.Replace(string(w.Body), "\n", "", -1)) | ||
| require.NotNil(t, extErr) | ||
| assert.EqualError(t, err, extErr.Error()) | ||
|
|
||
| SetDebug(false) | ||
| w = newTestWriter() | ||
| WriteError(w, err) | ||
| extErr = ReadError(w.StatusCode, w.Body) | ||
| assert.Equal(t, http.StatusInternalServerError, w.StatusCode) | ||
| assert.Regexp(t, ".*.snap.*", strings.Replace(string(w.Body), "\n", "", -1)) | ||
| require.NotNil(t, extErr) | ||
| assert.EqualError(t, err, extErr.Error()) | ||
|
codingllama marked this conversation as resolved.
|
||
| } | ||
|
|
||
| func TestAggregateConvertsToCommonErrors(t *testing.T) { | ||
| testCases := []struct { | ||
| Err error | ||
| Predicate func(error) bool | ||
| RoundtripPredicate func(error) bool | ||
| StatusCode int | ||
| comment string | ||
| }{ | ||
| { | ||
| comment: "Aggregate unwraps to first aggregated error", | ||
| Err: NewAggregate( | ||
| BadParameter("invalid value of foo"), | ||
| LimitExceeded("limit exceeded"), | ||
| ), | ||
| Predicate: IsAggregate, | ||
| RoundtripPredicate: IsBadParameter, | ||
| StatusCode: http.StatusBadRequest, | ||
| }, | ||
| { | ||
| comment: "Nested aggregate unwraps recursively", | ||
| Err: NewAggregate( | ||
| NewAggregate( | ||
| BadParameter("invalid value of foo"), | ||
| LimitExceeded("limit exceeded"), | ||
| ), | ||
| ), | ||
| Predicate: IsAggregate, | ||
| RoundtripPredicate: IsBadParameter, | ||
| StatusCode: http.StatusBadRequest, | ||
| }, | ||
| } | ||
| for _, testCase := range testCases { | ||
| SetDebug(true) | ||
| err := testCase.Err | ||
|
|
||
| assert.Regexp(t, ".*.trace_crypto_test.go.*", line(DebugReport(err)), testCase.comment) | ||
| assert.True(t, testCase.Predicate(err), testCase.comment) | ||
|
|
||
| w := newTestWriter() | ||
| WriteError(w, err) | ||
| outErr := ReadError(w.StatusCode, w.Body) | ||
| assert.True(t, testCase.RoundtripPredicate(outErr), testCase.comment) | ||
|
|
||
| SetDebug(false) | ||
| w = newTestWriter() | ||
| WriteError(w, err) | ||
| outErr = ReadError(w.StatusCode, w.Body) | ||
| assert.True(t, testCase.RoundtripPredicate(outErr), testCase.comment) | ||
| } | ||
| } | ||
Oops, something went wrong.
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.
I haven't used goda because I'm not sure of which version of go that would require, and using
go listis good enough for this specific purpose.