- Notifications
You must be signed in to change notification settings - Fork921
fix: allow posting licenses that will be valid in future#14491
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
9 changes: 8 additions & 1 deletionenterprise/coderd/coderdenttest/coderdenttest.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
41 changes: 39 additions & 2 deletionsenterprise/coderd/license/license.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 |
---|---|---|
@@ -287,6 +287,8 @@ var ( | ||
ErrInvalidVersion = xerrors.New("license must be version 3") | ||
ErrMissingKeyID = xerrors.Errorf("JOSE header must contain %s", HeaderKeyID) | ||
ErrMissingLicenseExpires = xerrors.New("license missing license_expires") | ||
ErrMissingExp = xerrors.New("exp claim missing or not parsable") | ||
ErrMultipleIssues = xerrors.New("license has multiple issues; contact support") | ||
) | ||
type Features map[codersdk.FeatureName]int64 | ||
@@ -336,7 +338,7 @@ func ParseRaw(l string, keys map[string]ed25519.PublicKey) (jwt.MapClaims, error | ||
return nil, xerrors.New("unable to parse Claims") | ||
} | ||
// ParseClaims validates araw JWT, and if valid, returns the claims. If | ||
// unparsable or invalid, it returns an error | ||
func ParseClaims(rawJWT string, keys map[string]ed25519.PublicKey) (*Claims, error) { | ||
tok, err := jwt.ParseWithClaims( | ||
@@ -348,18 +350,53 @@ func ParseClaims(rawJWT string, keys map[string]ed25519.PublicKey) (*Claims, err | ||
if err != nil { | ||
return nil, err | ||
} | ||
return validateClaims(tok) | ||
} | ||
func validateClaims(tok *jwt.Token) (*Claims, error) { | ||
if claims, ok := tok.Claims.(*Claims); ok { | ||
if claims.Version != uint64(CurrentVersion) { | ||
return nil, ErrInvalidVersion | ||
} | ||
if claims.LicenseExpires == nil { | ||
return nil, ErrMissingLicenseExpires | ||
} | ||
if claims.ExpiresAt == nil { | ||
return nil, ErrMissingExp | ||
} | ||
return claims, nil | ||
} | ||
return nil, xerrors.New("unable to parse Claims") | ||
} | ||
// ParseClaimsIgnoreNbf validates a raw JWT, but ignores `nbf` claim. If otherwise valid, it returns | ||
// the claims. If unparsable or invalid, it returns an error. Ignoring the `nbf` (not before) is | ||
// useful to determine if a JWT _will_ become valid at any point now or in the future. | ||
func ParseClaimsIgnoreNbf(rawJWT string, keys map[string]ed25519.PublicKey) (*Claims, error) { | ||
tok, err := jwt.ParseWithClaims( | ||
rawJWT, | ||
&Claims{}, | ||
keyFunc(keys), | ||
jwt.WithValidMethods(ValidMethods), | ||
) | ||
var vErr *jwt.ValidationError | ||
if xerrors.As(err, &vErr) { | ||
// zero out the NotValidYet error to check if there were other problems | ||
vErr.Errors = vErr.Errors & (^jwt.ValidationErrorNotValidYet) | ||
if vErr.Errors != 0 { | ||
spikecurtis marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
// There are other errors besides not being valid yet. We _could_ go | ||
// through all the jwt.ValidationError bits and try to work out the | ||
// correct error, but if we get here something very strange is | ||
// going on so let's just return a generic error that says to get in | ||
// touch with our support team. | ||
return nil, ErrMultipleIssues | ||
spikecurtis marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
} | ||
} else if err != nil { | ||
return nil, err | ||
} | ||
return validateClaims(tok) | ||
} | ||
func keyFunc(keys map[string]ed25519.PublicKey) func(*jwt.Token) (interface{}, error) { | ||
return func(j *jwt.Token) (interface{}, error) { | ||
keyID, ok := j.Header[HeaderKeyID].(string) | ||
32 changes: 11 additions & 21 deletionsenterprise/coderd/licenses.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
48 changes: 48 additions & 0 deletionsenterprise/coderd/licenses_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
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
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.