Skip to content
Next Next commit
neuron fixes
  • Loading branch information
Abhishek Singh authored and Abhishek Singh committed Oct 28, 2025
commit 5d4940ccd98f8c02e5781e7fef1e72b6a3efaa08
50 changes: 47 additions & 3 deletions adapters/vault/vault.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/abhissng/neuron/utils/cryptography"
"github.com/abhissng/neuron/utils/helpers"
infisical "github.com/infisical/go-sdk"
"github.com/infisical/go-sdk/packages/models"
)

// Constants for prefixes and timeouts
Expand Down Expand Up @@ -91,8 +92,20 @@ func NewVault(opts ...Option) *Vault {
}

// === Backend Retrieval Functions ===
func (v *Vault) retrieveInfisicalSecret(key string, list []*models.Secret) (string, error) {
if len(list) == 0 {
return "", errors.New("infisical secret list is empty")
}
for _, secret := range list {
if secret.SecretKey == key {
return secret.SecretValue, nil
}
}
return "", errors.New("infisical secret not found")
}

func (v *Vault) retrieveInfisicalSecret(key string) (string, error) {
// Retrieve a single secret from Infisical
func (v *Vault) RetrieveInfisicalSingleSecret(key string) (string, error) {
if v.infisicalClient == nil {
return "", errors.New("infisical client not initialized")
}
Expand All @@ -113,6 +126,29 @@ func (v *Vault) retrieveInfisicalSecret(key string) (string, error) {
return secret.SecretValue, nil
}

// Retrieve all secrets from Infisical
func (v *Vault) retrieveInfisicalSecrets() ([]*models.Secret, error) {
if v.infisicalClient == nil {
return nil, errors.New("infisical client not initialized")
}
secrets, err := v.infisicalClient.Secrets().List(infisical.ListSecretsOptions{
ProjectID: v.projectID,
Environment: v.env,
SecretPath: v.path,
AttachToProcessEnv: false,
Recursive: true,
})
if err != nil {
helpers.Println(constant.ERROR, "Error retrieving Infisical secrets: ", err)
return nil, fmt.Errorf("failed to list secrets: %w", err)
}
var secretList []*models.Secret
for _, secret := range secrets {
secretList = append(secretList, &secret)
}
return secretList, nil
}

func (v *Vault) retrieveAWSKMSSecret(ctx context.Context, secretId string) (string, error) {
if v.awsClient.GetKMSClient() == nil {
return "", errors.New("AWS KMS client not initialized")
Expand Down Expand Up @@ -186,7 +222,11 @@ func (v *Vault) FetchVaultValue(key string) (string, error) {
// source = "Infisical"
actualKey = strings.TrimPrefix(key, InfisicalPrefix)
// helpers.Println(constant.DEBUG, "Fetching from", source, "(explicit prefix) - Key:", actualKey)
return v.retrieveInfisicalSecret(actualKey)
secrets, err := v.retrieveInfisicalSecrets()
if err != nil {
return "", err
}
return v.retrieveInfisicalSecret(actualKey, secrets)
case strings.HasPrefix(key, AWSKMSPrefix):
// source = "AWS KMS"
actualKey = strings.TrimPrefix(key, AWSKMSPrefix)
Expand All @@ -200,7 +240,11 @@ func (v *Vault) FetchVaultValue(key string) (string, error) {
return v.retrieveAWSParameterStoreSecret(ctx, actualKey, true)
}
// helpers.Println(constant.DEBUG, "Fetching from", source, "(default) - Key:", actualKey)
return v.retrieveInfisicalSecret(actualKey)
secrets, err := v.retrieveInfisicalSecrets()
if err != nil {
return "", err
}
return v.retrieveInfisicalSecret(actualKey, secrets)
}
}

Expand Down
26 changes: 26 additions & 0 deletions utils/cryptography/helpers.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package cryptography

import (
"crypto/rand"
"crypto/subtle"
"encoding/base64"
"encoding/binary"
"fmt"

"golang.org/x/crypto/blake2s"
Expand Down Expand Up @@ -105,3 +107,27 @@ func Generate256BitHash(key, data []byte) (string, error) {
func CompareHash(hash1, hash2 string) bool {
return subtle.ConstantTimeCompare([]byte(hash1), []byte(hash2)) == 1
}

// Generate16BitKeyString returns a random 16-bit key as a string
func Generate16BitKeyString() (string, error) {
var b [2]byte
_, err := rand.Read(b[:])
if err != nil {
return "", err
}
key := binary.BigEndian.Uint16(b[:])
//return fmt.Sprintf("%d", key), nil // decimal string
return fmt.Sprintf("%04X", key), nil // uncomment for hex string
}

// Generate32BitKeyString returns a random 32-bit key as a string
func Generate32BitKeyString() (string, error) {
var b [4]byte
_, err := rand.Read(b[:])
if err != nil {
return "", err
}
key := binary.BigEndian.Uint32(b[:])
// return fmt.Sprintf("%d", key), nil // decimal string
return fmt.Sprintf("%08X", key), nil // uncomment for hex string
}
5 changes: 4 additions & 1 deletion utils/timeutil/timeutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,9 @@ func (t *TimeWrapper) AddDate(years, months, days int) *TimeWrapper {

// GetCurrentTimeIn returns current time in given timezone
func GetCurrentTimeIn(timezone string) time.Time {
location, _ := LoadLocation(timezone)
location, err := LoadLocation(timezone)
if err != nil {
return time.Now().UTC()
}
return Now().In(location).Time
}