@@ -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)
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+
243274func validatePublicVarName (name string ) error {
244275 if name == "" {
245276 return fmt .Errorf ("public variable name cannot be empty" )
0 commit comments