-
Notifications
You must be signed in to change notification settings - Fork 21
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
Improving --signer-fulcio-token
flag to accept both path and raw token string
#82
Merged
ChaosInTheCRD
merged 16 commits into
in-toto:main
from
ChaosInTheCRD:improving-token-flag
Dec 11, 2023
Merged
Changes from 7 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
98b5e95
modified `--signer-fulcio-token` flag to accept either a path to a token
ChaosInTheCRD 1c29560
modified `--signer-fulcio-token` flag to accept either a path to a token
ChaosInTheCRD f364321
adding an unhappy path to the tests
ChaosInTheCRD 4cb1a5f
Merge branch 'improving-token-flag' of github.com:ChaosInTheCRD/go-wi…
ChaosInTheCRD e380188
updated function and description
ChaosInTheCRD 89e5584
updated function and description
ChaosInTheCRD cc97b17
Merge branch 'improving-token-flag' of github.com:ChaosInTheCRD/go-wi…
ChaosInTheCRD 250b0e5
Merge branch 'main' into improving-token-flag
ChaosInTheCRD 70d18d0
removing ineffectual assignment in test
ChaosInTheCRD 2c2dc7a
Merge branch 'main' of github.com:in-toto/go-witness into improving-t…
ChaosInTheCRD d9e960f
updated to add token path flag and remove idToken function magic
ChaosInTheCRD 51ac258
removing ineffectual assignments
ChaosInTheCRD 990446e
removing whitespace
ChaosInTheCRD e63d8e7
fixing small issue and adding test to makefile to speed things up
ChaosInTheCRD f059865
Merge branch 'main' into improving-token-flag
ChaosInTheCRD 638888e
Merge branch 'main' into improving-token-flag
ChaosInTheCRD 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 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 @@ | ||
eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VySWQiOiJhYmNkMTIzIiwiZXhwaXJ5IjoxNjQ2NjM1NjExMzAxfQ.3Thp81rDFrKXr3WrY1MyMnNK8kKoZBX9lg-JwFznR-M |
This file contains 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 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 |
---|---|---|
|
@@ -24,6 +24,7 @@ | |
"fmt" | ||
"log" | ||
"net" | ||
"os" | ||
"strings" | ||
"testing" | ||
"time" | ||
|
@@ -33,6 +34,7 @@ | |
fulciopb "github.com/sigstore/fulcio/pkg/generated/protobuf" | ||
"github.com/stretchr/testify/require" | ||
"go.step.sm/crypto/jose" | ||
"path/filepath" | ||
|
||
"google.golang.org/grpc" | ||
"gopkg.in/square/go-jose.v2/jwt" | ||
|
@@ -76,6 +78,37 @@ | |
require.NotNil(t, client) | ||
} | ||
|
||
func TestIDToken(t *testing.T) { | ||
// test when supplying a raw token, the same token is returned | ||
tok := generateTestToken("[email protected]", "testsubject") | ||
out, err := idToken(tok) | ||
require.NoError(t, err) | ||
require.Equal(t, tok, out) | ||
|
||
// test when supplying a path, a valid token is returned | ||
// NOTE: this function could be refactored to accept a fileSystem or io.Reader so reading the file can be mocked, | ||
// but unsure if this is the way we want to go for now | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like this idea. The less code we have in the test (i.e. composing a file path) the better. I think you could also do a relative path: |
||
wd, err := os.Getwd() | ||
if err != nil { | ||
t.Fatalf("failed to get working directory: %v", err) | ||
} | ||
rootDir := filepath.Dir(filepath.Dir(wd)) | ||
tok = filepath.Join(rootDir, "hack", "test.token") | ||
testTok, err := os.ReadFile(tok) | ||
if err != nil { | ||
t.Fatalf("failed to read test token file: %v", err) | ||
} | ||
|
||
out, err = idToken(tok) | ||
require.NoError(t, err) | ||
require.Equal(t, string(testTok), out) | ||
|
||
// test that when neither valid token nor a path is supplied, an error is returned | ||
tok = "test" | ||
out, err = idToken(tok) | ||
require.Error(t, err) | ||
} | ||
|
||
type dummyCAClientService struct { | ||
client fulciopb.CAClient | ||
server *grpc.Server | ||
|
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.
https://dave.cheney.net/2019/07/09/clear-is-better-than-clever
https://go-proverbs.github.io/
The design of a function to fall back from one behavior to another can be both practical and user-friendly, but it also has potential drawbacks that need to be considered. Here are some points to consider regarding the design of the idToken function:
Pros:
User Convenience: The function provides flexibility by accepting either a raw token or a file path. This can be convenient for users with different forms of tokens.
Simplicity: The function abstracts away the details of how the token is obtained, simplifying the interface for the caller.
Fallback Mechanism: A fallback mechanism can be a good defensive programming practice, ensuring the function has an alternative way to proceed if the first method fails.
Cons:
Ambiguity: The dual-purpose nature of the function's argument can lead to ambiguity. It's not immediately clear from the function signature what the expected input is.
Error Handling: If the input is neither a valid token nor a valid file path, the error returned may not be informative enough to help the user understand the issue.
Security Concerns: Automatically treating the input as a file path if it's not a valid token could lead to security issues, such as unintentional file reads.
Single Responsibility Principle: The function parses a token and reads a file. This goes against the Single Responsibility Principle, which suggests that a function should do one thing only.
Testing Complexity: The function may be harder to test because it has multiple execution paths depending on the input.
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.
@naveensrinivasan - thank you so much for this comment! I was thinking exactly this when I was creating the PR. @mikhailswift @jkjell @kairoaraujo @colek42 and others, it would be good to get your perspectives on this one so we can decide a way to proceed with the PR 😄
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.
That’s a good comment.
I’d definitely recommend splitting up the functionality. Also from a security perspective would be more defensive to treat the suspected JWS as only ever being a JWS even if it fails to parse and have a different path for matching to a file.
Perhaps a different flag or envvar or however else it’s getting picked up.
Being explicit is better than implicit in this case I think.
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.
Agree with @fkautz . There's a certain nicety when things just happen for you implicitly, which is always attractive to make things convenient, but I tend to lean toward explicit options being better for use cases like this, especially due to the logic of "well this isn't a token, so let's try opening it like a file" being particularly scary.