- Notifications
You must be signed in to change notification settings - Fork928
chore: shorten provisioner key#14017
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
Uh oh!
There was an error while loading.Please reload this page.
Changes from1 commit
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
- Loading branch information
Uh oh!
There was an error while loading.Please reload this page.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.
Uh oh!
There was an error while loading.Please reload this page.
Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.
Uh oh!
There was an error while loading.Please reload this page.
Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.
Uh oh!
There was an error while loading.Please reload this page.
Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.
Uh oh!
There was an error while loading.Please reload this page.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -71,16 +71,17 @@ func ExtractProvisionerDaemonAuthenticated(opts ExtractProvisionerAuthConfig) fu | ||
return | ||
} | ||
err := provisionerkey.Validate(key) | ||
if err != nil { | ||
handleOptional(http.StatusBadGateway, codersdk.Response{ | ||
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.
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. This error has a detail that specifies the length must be 64, and I thought it felt more like a 400 in that regard. 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. omg I meant | ||
Message: "provisioner daemon key invalid", | ||
Detail: err.Error(), | ||
}) | ||
return | ||
} | ||
hashedKey := provisionerkey.HashSecret(key) | ||
// nolint:gocritic // System must check if the provisioner key is valid. | ||
pk, err := opts.DB.GetProvisionerKeyByHashedSecret(dbauthz.AsSystemRestricted(ctx),hashedKey) | ||
if err != nil { | ||
if httpapi.Is404Error(err) { | ||
handleOptional(http.StatusUnauthorized, codersdk.Response{ | ||
@@ -90,12 +91,13 @@ func ExtractProvisionerDaemonAuthenticated(opts ExtractProvisionerAuthConfig) fu | ||
} | ||
handleOptional(http.StatusInternalServerError, codersdk.Response{ | ||
Message: "get provisioner daemon key", | ||
Detail: err.Error(), | ||
}) | ||
return | ||
} | ||
if provisionerkey.Compare(pk.HashedSecret,hashedKey) { | ||
handleOptional(http.StatusUnauthorized, codersdk.Response{ | ||
Message: "provisioner daemon key invalid", | ||
}) | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -3,8 +3,6 @@ package provisionerkey | ||
import ( | ||
"crypto/sha256" | ||
"crypto/subtle" | ||
"github.com/google/uuid" | ||
"golang.org/x/xerrors" | ||
@@ -15,40 +13,31 @@ import ( | ||
) | ||
func New(organizationID uuid.UUID, name string, tags map[string]string) (database.InsertProvisionerKeyParams, string, error) { | ||
secret, err := cryptorand.String(64) | ||
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. 64 is probably longer than we need if we are using alpha (lower & upper) + numbers. When we using hex strings, technically they are case insensitive, so you only get 16 options vs the now 62 (26 lower, 26 upper, 10 numeric) You only need ~43 characters to match the same search space as 64 hex characters. We only use22 characters for our api keys (I bet we copied someone else here). It does not really matter, but if we are shortening our keys, we can at least shorten them to 43 and keep the same level of security as 64 hex characters. Which is super common.
Thoughts on going even shorter? 64 is such an excessive level of security. 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. Of course, I was leaning on better safe than sorry but let's go short - 32 sound good? 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. 32 is ~190 bits of entropy. 43 gets us to 256, which is common in symmetric key encryption. At the end of the day, 32 vs 43 feels completely arbitrary and probably sufficient, but 43 feels like "random" than "32" in the fact it lines up with 256bit. 🤷♂️ I don't think 43 looks too bad. That's just my 2c. 43 characters long
32
64
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. 43 it is 😄 | ||
if err != nil { | ||
return database.InsertProvisionerKeyParams{}, "", xerrors.Errorf("generatesecret: %w", err) | ||
} | ||
if tags == nil { | ||
tags = map[string]string{} | ||
} | ||
return database.InsertProvisionerKeyParams{ | ||
ID:uuid.New(), | ||
CreatedAt: dbtime.Now(), | ||
OrganizationID: organizationID, | ||
Name: name, | ||
HashedSecret:HashSecret(secret), | ||
Tags: tags, | ||
},secret, nil | ||
} | ||
func Validate(token string) error { | ||
if len(token) != 64 { | ||
return xerrors.Errorf("must be 64 characters") | ||
} | ||
return nil | ||
} | ||
func HashSecret(secret string) []byte { | ||