@@ -15,7 +15,9 @@ import (
1515 xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1"
1616)
1717
18- // SecretRefToJSONRawMessage extracts parameters/credentials from a secret reference.
18+ // ExtractSecret extracts parameters/credentials from a secret reference.
19+ // If a key is specified, returns the raw value for that key.
20+ // If no key is specified, returns all secret data as nested JSON/YAML.
1921func ExtractSecret (ctx context.Context , kube k8s.Client , sr * xpv1.SecretReference , key string ) ([]byte , error ) {
2022 if sr == nil {
2123 return nil , nil
@@ -34,14 +36,18 @@ func ExtractSecret(ctx context.Context, kube k8s.Client, sr *xpv1.SecretReferenc
3436 return nil , nil
3537 }
3638
37- // if key is not specified, return all data from the secret
38- cred := make (map [string ]string )
39+ // if key is not specified, return all data from the secret, also string or nested JSON
40+ data := make (map [string ]interface {} )
3941 for k , v := range secret .Data {
40- cred [k ] = string (v )
41- }
42- buf , err := json .Marshal (cred )
43- if err != nil {
44- return nil , err
42+ // Try to parse as JSON first
43+ var jsonValue interface {}
44+ if err := json .Unmarshal (v , & jsonValue ); err == nil {
45+ data [k ] = jsonValue
46+ } else {
47+ // If not JSON, store as string
48+ data [k ] = string (v )
49+ }
4550 }
46- return buf , nil
51+
52+ return json .Marshal (data )
4753}
0 commit comments