Skip to content

Commit e3c9919

Browse files
committed
Fixup: Prevent public var and secret overlap
1 parent 40a7fc0 commit e3c9919

2 files changed

Lines changed: 34 additions & 0 deletions

File tree

cmd/rofl/mgmt.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,7 @@ var (
652652
if err := appID.UnmarshalText([]byte(deployment.AppID)); err != nil {
653653
cobra.CheckErr(fmt.Errorf("malformed ROFL app ID: %w", err))
654654
}
655+
cobra.CheckErr(checkNoPublicVarNamed(deployment.Metadata, secretName))
655656

656657
// Establish connection with the target network.
657658
ctx := context.Background()
@@ -769,6 +770,8 @@ var (
769770

770771
var imported, updated int
771772
for _, name := range names {
773+
cobra.CheckErr(checkNoPublicVarNamed(deployment.Metadata, name))
774+
772775
value := []byte(entries[name])
773776

774777
encValue, err := buildRofl.EncryptSecret(name, value, appCfg.SEK)

cmd/rofl/public_var.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@ import (
55
"io"
66
"os"
77
"sort"
8+
"strings"
89
"unicode/utf8"
910

1011
"github.com/spf13/cobra"
1112

1213
buildDotenv "github.com/oasisprotocol/cli/build/dotenv"
14+
buildRofl "github.com/oasisprotocol/cli/build/rofl"
1315
"github.com/oasisprotocol/cli/cmd/common"
1416
roflCommon "github.com/oasisprotocol/cli/cmd/rofl/common"
1517
)
@@ -42,6 +44,7 @@ var (
4244
if err != nil {
4345
cobra.CheckErr(err)
4446
}
47+
cobra.CheckErr(checkNoSecretNamed(deployment.Secrets, publicVarName))
4548

4649
// Read public variable.
4750
var publicVarValueRaw []byte
@@ -139,6 +142,7 @@ var (
139142
if err != nil {
140143
cobra.CheckErr(err)
141144
}
145+
cobra.CheckErr(checkNoSecretNamed(deployment.Secrets, name))
142146

143147
if existing, ok := deployment.Metadata[key]; ok {
144148
if existing == entries[name] {
@@ -240,6 +244,33 @@ func publicVarMetadataKey(name string) (string, error) {
240244
return publicVarMetadataPrefix + name, nil
241245
}
242246

247+
// checkNoSecretNamed returns an error if a secret exposed under the given environment variable
248+
// name exists among the secrets.
249+
func checkNoSecretNamed(secrets []*buildRofl.SecretConfig, name string) error {
250+
for _, sc := range secrets {
251+
if secretEnvVarName(sc.Name) == name {
252+
return fmt.Errorf("the secret named '%s' for deployment '%s' already exists", sc.Name, roflCommon.DeploymentName)
253+
}
254+
}
255+
return nil
256+
}
257+
258+
// checkNoPublicVarNamed returns an error if a public variable exposed under the same environment
259+
// variable name as a secret with the given name exists in the deployment metadata.
260+
func checkNoPublicVarNamed(metadata map[string]string, name string) error {
261+
envName := secretEnvVarName(name)
262+
if _, ok := metadata[publicVarMetadataPrefix+envName]; ok {
263+
return fmt.Errorf("the public variable named '%s' for deployment '%s' already exists", envName, roflCommon.DeploymentName)
264+
}
265+
return nil
266+
}
267+
268+
// secretEnvVarName returns the environment variable name under which rofl-containers exposes a
269+
// secret with the given name.
270+
func secretEnvVarName(name string) string {
271+
return strings.ToUpper(strings.ReplaceAll(name, " ", "_"))
272+
}
273+
243274
func validatePublicVarName(name string) error {
244275
if name == "" {
245276
return fmt.Errorf("public variable name cannot be empty")

0 commit comments

Comments
 (0)