-
Notifications
You must be signed in to change notification settings - Fork 892
CLI: coder licenses list #3686
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
CLI: coder licenses list #3686
Changes from all commits
Commits
Show all changes
2 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,74 @@ | ||
package coderd | ||
|
||
import ( | ||
"context" | ||
"crypto/ed25519" | ||
"crypto/rand" | ||
"net/http" | ||
"testing" | ||
"time" | ||
|
||
"github.com/golang-jwt/jwt/v4" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/coder/coder/coderd/coderdtest" | ||
"github.com/coder/coder/coderd/rbac" | ||
"github.com/coder/coder/codersdk" | ||
"github.com/coder/coder/testutil" | ||
) | ||
|
||
// TestAuthorizeAllEndpoints will check `authorize` is called on every endpoint registered. | ||
// these tests patch the map of license keys, so cannot be run in parallel | ||
// nolint:paralleltest | ||
func TestAuthorizeAllEndpoints(t *testing.T) { | ||
pubKey, privKey, err := ed25519.GenerateKey(rand.Reader) | ||
require.NoError(t, err) | ||
keyID := "testing" | ||
oldKeys := keys | ||
defer func() { | ||
t.Log("restoring keys") | ||
keys = oldKeys | ||
}() | ||
keys = map[string]ed25519.PublicKey{keyID: pubKey} | ||
|
||
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong) | ||
defer cancel() | ||
a := coderdtest.NewAuthTester(ctx, t, &coderdtest.Options{APIBuilder: NewEnterprise}) | ||
|
||
// We need a license in the DB, so that when we call GET api/v2/licenses there is one in the | ||
// list to check authz on. | ||
claims := &Claims{ | ||
RegisteredClaims: jwt.RegisteredClaims{ | ||
Issuer: "test@coder.test", | ||
IssuedAt: jwt.NewNumericDate(time.Now()), | ||
NotBefore: jwt.NewNumericDate(time.Now()), | ||
ExpiresAt: jwt.NewNumericDate(time.Now().Add(2 * time.Hour)), | ||
}, | ||
LicenseExpires: jwt.NewNumericDate(time.Now().Add(time.Hour)), | ||
AccountType: AccountTypeSalesforce, | ||
AccountID: "testing", | ||
Version: CurrentVersion, | ||
Features: Features{ | ||
UserLimit: 0, | ||
AuditLog: 1, | ||
}, | ||
} | ||
lic, err := makeLicense(claims, privKey, keyID) | ||
require.NoError(t, err) | ||
_, err = a.Client.AddLicense(ctx, codersdk.AddLicenseRequest{ | ||
License: lic, | ||
}) | ||
require.NoError(t, err) | ||
|
||
skipRoutes, assertRoute := coderdtest.AGPLRoutes(a) | ||
assertRoute["POST:/api/v2/licenses"] = coderdtest.RouteCheck{ | ||
AssertAction: rbac.ActionCreate, | ||
AssertObject: rbac.ResourceLicense, | ||
} | ||
assertRoute["GET:/api/v2/licenses"] = coderdtest.RouteCheck{ | ||
StatusCode: http.StatusOK, | ||
AssertAction: rbac.ActionRead, | ||
AssertObject: rbac.ResourceLicense, | ||
} | ||
a.Test(ctx, assertRoute, skipRoutes) | ||
} |
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -12,11 +12,12 @@ import ( | |
"strings" | ||
"time" | ||
|
||
"cdr.dev/slog" | ||
"github.com/go-chi/chi/v5" | ||
"github.com/golang-jwt/jwt/v4" | ||
"golang.org/x/xerrors" | ||
|
||
"cdr.dev/slog" | ||
|
||
"github.com/coder/coder/coderd" | ||
"github.com/coder/coder/coderd/database" | ||
"github.com/coder/coder/coderd/httpapi" | ||
|
@@ -253,7 +254,7 @@ func decodeClaims(l database.License) (jwt.MapClaims, error) { | |
if len(parts) != 3 { | ||
return nil, xerrors.Errorf("Unable to parse license %d as JWT", l.ID) | ||
} | ||
cb, err := base64.URLEncoding.DecodeString(parts[1]) | ||
cb, err := base64.RawURLEncoding.DecodeString(parts[1]) | ||
Comment on lines
-256
to
+257
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. For those who had to look this up, there are four kinds of base64 encoding [1] [2]:
[1] https://pkg.go.dev/encoding/base64#pkg-variables |
||
if err != nil { | ||
return nil, xerrors.Errorf("Unable to decode license %d claims: %w", l.ID, err) | ||
} | ||
|
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.
(nit, observation, non-blocking): This seems like something that we could shoe-horn into a
coderdtest
convenience function, as it might be useful elsewhere.Uh oh!
There was an error while loading. Please reload this page.
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.
yeah, it's gonna be needed for basically any test of enterprise-licensed features on the API...I take this back. I have a better idea for enterprise features. But, it's needed for testing the parts of the API that deal with actual licenses.