This repository has been archived by the owner on Mar 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Signed-off-by: pratikjagrut <26519653+pratikjagrut@users.noreply.github.com>
- Loading branch information
1 parent
05d349b
commit a2b288d
Showing
5 changed files
with
102 additions
and
5 deletions.
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
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,32 @@ | ||
package secrets | ||
|
||
import ( | ||
"crypto/rand" | ||
"math/big" | ||
) | ||
|
||
// GenerateRandomSecret generates a random secret with the specified length | ||
// using a mix of uppercase letters, lowercase letters, numbers, and special characters. | ||
func GenerateRandomSecret(length int) (string, error) { | ||
const ( | ||
uppercase = "ABCDEFGHJKLMNPQRSTUVWXYZ" | ||
lowercase = "abcdefghijkmnopqrstuvwxyz" | ||
numbers = "23456789" | ||
special = "!@#$%^&*_-=+" | ||
) | ||
|
||
// Create a pool of characters to choose from | ||
pool := uppercase + lowercase + numbers + special | ||
|
||
// Generate a random secret by randomly selecting characters from the pool | ||
secret := make([]byte, length) | ||
for i := 0; i < length; i++ { | ||
index, err := rand.Int(rand.Reader, big.NewInt(int64(len(pool)))) | ||
if err != nil { | ||
return "", err | ||
} | ||
secret[i] = pool[index.Int64()] | ||
} | ||
|
||
return string(secret), nil | ||
} |
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
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,41 @@ | ||
package secrets | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/acorn-io/mink/pkg/strategy" | ||
"github.com/acorn-io/mink/pkg/types" | ||
apiv1 "github.com/acorn-io/runtime/pkg/apis/api.acorn.io/v1" | ||
sec "github.com/acorn-io/runtime/pkg/secrets" | ||
) | ||
|
||
type defaultSecretGenerateStrategy struct { | ||
strategy strategy.CompleteStrategy | ||
} | ||
|
||
func (d *defaultSecretGenerateStrategy) Create(ctx context.Context, object types.Object) (types.Object, error) { | ||
secret := object.(*apiv1.Secret) | ||
// If the secret is of type 'basic' and data is empty, | ||
// default username and password values are set. | ||
if secret.Type == "basic" && secret.Data == nil { | ||
username, err := sec.GenerateRandomSecret(8) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
password, err := sec.GenerateRandomSecret(16) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
secret.Data = map[string][]byte{} | ||
|
||
secret.Data["username"] = []byte(username) | ||
secret.Data["password"] = []byte(password) | ||
} | ||
return d.strategy.Create(ctx, secret) | ||
} | ||
|
||
func (d *defaultSecretGenerateStrategy) New() types.Object { | ||
return &apiv1.Secret{} | ||
} |