Skip to content

Commit

Permalink
Merge pull request #68 from skit-ai/generate_static_key
Browse files Browse the repository at this point in the history
Use Static Data Key for Offline deployment
  • Loading branch information
vipul-sharma20 authored May 30, 2024
2 parents 6975c30 + 7e895ff commit f5324c8
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions crypto/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/hashicorp/vault/api"
auth "github.com/hashicorp/vault/api/auth/approle"
"github.com/skit-ai/vcore/env"
)

// Read Env Vars
Expand All @@ -20,12 +21,23 @@ var vault_secret_id string = os.Getenv("VAULT_SECRET_ID")
var vault_approle_mountpath string = os.Getenv("VAULT_APPROLE_MOUNTPATH")
var vault_data_key_name string = os.Getenv("VAULT_DATA_KEY_NAME")
var encrypted_data_key string = os.Getenv("ENCRYPTED_DATA_KEY")
var use_static_data_key bool = env.Bool("USE_STATIC_DATA_KEY", false)
var static_data_key string = env.String("STATIC_DATA_KEY", "")

// Other Global Variables

var data_key []byte
var dataKeyCache map[string][]byte = map[string][]byte{}

func isValidBase64(static_data_key string) bool {
_, err := base64.StdEncoding.DecodeString(static_data_key)
return err == nil
}

func getByteString(static_data_key string) []byte {
return []byte(static_data_key)
}

// Vault functions
func getApproleAuth() *auth.AppRoleAuth {
// Check if vault_approle_mountpath has a value
Expand Down Expand Up @@ -136,9 +148,13 @@ func getDataKey(encrypted_data_key_ string, clientId string) (data_key_ []byte)
// Crypto functions
func newCipherAESGCMObject(data_key_b64_str string, clientId string) (gcm cipher.AEAD, err error) {

var data_key []byte
// Get data key
data_key := getDataKey(data_key_b64_str, clientId)

if use_static_data_key && isValidBase64(static_data_key) {
data_key = getByteString(static_data_key)
} else {
data_key = getDataKey(data_key_b64_str, clientId)
}
// Generate new aes cipher using our 32 byte key
c, err := aes.NewCipher(data_key)
if err != nil {
Expand Down

0 comments on commit f5324c8

Please sign in to comment.