package plugin import ( "encoding/base64" "errors" "strings" ) const protectedSecretPrefix = "dpapi:v1:" var ErrSecretNotProtected = errors.New("stored secret is not protected") // ErrSecretEmpty marks a secret setting that was never saved for the requested // scope. Callers that may fall back to another VM (a temporary clone reading // its source) match it with errors.Is instead of comparing text. var ErrSecretEmpty = errors.New("secret setting is empty") func protectSettingSecret(value string) (string, error) { if value == "" || strings.HasPrefix(value, protectedSecretPrefix) { return value, nil } protected, err := protectSecretBytes([]byte(value)) if err != nil { return "", err } return protectedSecretPrefix + base64.StdEncoding.EncodeToString(protected), nil } func unprotectSettingSecret(value string) (string, error) { if !strings.HasPrefix(value, protectedSecretPrefix) { return "", ErrSecretNotProtected } encoded := strings.TrimPrefix(value, protectedSecretPrefix) protected, err := base64.StdEncoding.DecodeString(encoded) if err != nil { return "", err } plain, err := unprotectSecretBytes(protected) if err != nil { return "", err } return string(plain), nil }