Skip to content

Commit

Permalink
leveraging crypto/rand package to generate access keys (#4577)
Browse files Browse the repository at this point in the history
Signed-off-by: Saranya-jena <saranya.jena@harness.io>
  • Loading branch information
Saranya-jena committed Apr 15, 2024
1 parent 7e6ac52 commit 5267d52
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1009,7 +1009,10 @@ func (in *infraService) ConfirmInfraRegistration(request model.InfraIdentity, r
}

if infra.AccessKey == request.AccessKey {
newKey := utils.RandomString(32)
newKey, err := utils.GenerateAccessKey(32)
if err != nil {
return &model.ConfirmInfraRegistrationResponse{IsInfraConfirmed: false}, err
}
time := time.Now().UnixMilli()
query := bson.D{{"infra_id", request.InfraID}}
update := bson.D{{"$unset", bson.D{{"token", ""}}}, {"$set", bson.D{{"access_key", newKey}, {"is_registered", true}, {"is_infra_confirmed", true}, {"updated_at", time}}}}
Expand Down
14 changes: 13 additions & 1 deletion chaoscenter/graphql/server/utils/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package utils

import (
"bytes"
crypto "crypto/rand"
"encoding/base64"
"fmt"
"math/rand"
Expand All @@ -25,7 +26,18 @@ func WriteHeaders(w *gin.ResponseWriter, statusCode int) {
(*w).WriteHeader(statusCode)
}

// RandomString generates random strings, can be used to create ids or random secrets
// GenerateAccessKey generates an access key by leveraging crypto/rand package
func GenerateAccessKey(length int) (string, error) {
b := make([]byte, length)
_, err := crypto.Read(b)
if err != nil {
return "", err
}

return base64.URLEncoding.EncodeToString(b), nil
}

// RandomString generates random strings, can be used to create ids
func RandomString(n int) string {
if n > 0 {
var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-")
Expand Down
8 changes: 4 additions & 4 deletions chaoscenter/web/src/views/Login/__tests__/LoginPage.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ describe('LoginPageView', () => {
test('With Dex Login Disabled', async () => {
const capabilitiesWithDexDisabled = {
dex: {
enabled: false,
},
enabled: false
}
};

render(
Expand All @@ -55,8 +55,8 @@ describe('LoginPageView', () => {
test('With Dex Login Enabled', async () => {
const capabilitiesWithDexEnabled = {
dex: {
enabled: true,
},
enabled: true
}
};
render(
<TestWrapper>
Expand Down

0 comments on commit 5267d52

Please sign in to comment.