From 6b002a7d08c07384f2163b71fd6f826fb20734fb Mon Sep 17 00:00:00 2001 From: LaMont Jones Date: Mon, 10 Jun 2024 10:58:44 -0700 Subject: [PATCH 1/2] build-flag: ensure release_config declaration exists If we are setting a flag for a release config in a map directory that doesn't yet declare that release config, this map directory needs to contribute to the release config. Bug: 345278765 Test: manual Change-Id: Ie4e74bce008c4c4fdc4bc16e3209f0d9ef9cf8a2 --- cmd/release_config/build_flag/main.go | 20 ++++++++++++++++++- cmd/release_config/release_config_lib/util.go | 5 +++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/cmd/release_config/build_flag/main.go b/cmd/release_config/build_flag/main.go index f74784bd8..e18044b4e 100644 --- a/cmd/release_config/build_flag/main.go +++ b/cmd/release_config/build_flag/main.go @@ -278,6 +278,23 @@ func SetCommand(configs *rc_lib.ReleaseConfigs, commonFlags Flags, cmd string, a valueDir = mapDir } + var updatedFiles []string + rcPath := filepath.Join(valueDir, "release_configs", fmt.Sprintf("%s.textproto", targetRelease)) + // Create the release config declaration only if necessary. + if _, err = os.Stat(rcPath); err != nil { + if err = os.MkdirAll(filepath.Dir(rcPath), 0775); err != nil { + return err + } + rcValue := &rc_proto.ReleaseConfig{ + Name: proto.String(targetRelease), + } + err = rc_lib.WriteMessage(rcPath, rcValue) + if err != nil { + return err + } + updatedFiles = append(updatedFiles, rcPath) + } + flagValue := &rc_proto.FlagValue{ Name: proto.String(name), Value: rc_lib.UnmarshalValue(value), @@ -297,7 +314,8 @@ func SetCommand(configs *rc_lib.ReleaseConfigs, commonFlags Flags, cmd string, a if err != nil { return err } - fmt.Printf("Updated: %s\n", flagPath) + updatedFiles = append(updatedFiles, flagPath) + fmt.Printf("Added/Updated: %s\n", strings.Join(updatedFiles, " ")) return nil } diff --git a/cmd/release_config/release_config_lib/util.go b/cmd/release_config/release_config_lib/util.go index b8824d19c..0a19efe01 100644 --- a/cmd/release_config/release_config_lib/util.go +++ b/cmd/release_config/release_config_lib/util.go @@ -83,6 +83,11 @@ func WriteMessage(path string, message proto.Message) (err error) { // error: any error encountered. func WriteFormattedMessage(path, format string, message proto.Message) (err error) { var data []byte + if _, err := os.Stat(filepath.Dir(path)); err != nil { + if err = os.MkdirAll(filepath.Dir(path), 0775); err != nil { + return err + } + } switch format { case "json": data, err = json.MarshalIndent(message, "", " ") From fcb2d377caa77ac83af040edbee70a5300765225 Mon Sep 17 00:00:00 2001 From: LaMont Jones Date: Mon, 10 Jun 2024 16:05:02 -0700 Subject: [PATCH 2/2] build-flag: support `set --redacted=true NAME` Simplify the process of redacting a flag. Bug: None Test: manual Change-Id: Ib0f26c9846141030e1f23a389044debc9945bbbf --- cmd/release_config/build_flag/main.go | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/cmd/release_config/build_flag/main.go b/cmd/release_config/build_flag/main.go index e18044b4e..160f2cf20 100644 --- a/cmd/release_config/build_flag/main.go +++ b/cmd/release_config/build_flag/main.go @@ -232,7 +232,7 @@ func GetCommand(configs *rc_lib.ReleaseConfigs, commonFlags Flags, cmd string, a } else { outputOneLine(arg, config.Name, "REDACTED", "%s") } - if isTrace { + if err == nil && isTrace { for _, trace := range config.FlagArtifacts[arg].Traces { fmt.Printf(" => \"%s\" in %s\n", rc_lib.MarshalValue(trace.Value), *trace.Source) } @@ -244,6 +244,8 @@ func GetCommand(configs *rc_lib.ReleaseConfigs, commonFlags Flags, cmd string, a func SetCommand(configs *rc_lib.ReleaseConfigs, commonFlags Flags, cmd string, args []string) error { var valueDir string + var redacted bool + var value string if len(commonFlags.targetReleases) > 1 { return fmt.Errorf("set command only allows one --release argument. Got: %s", strings.Join(commonFlags.targetReleases, " ")) } @@ -251,13 +253,20 @@ func SetCommand(configs *rc_lib.ReleaseConfigs, commonFlags Flags, cmd string, a setFlags := flag.NewFlagSet("set", flag.ExitOnError) setFlags.StringVar(&valueDir, "dir", "", "Directory in which to place the value") + setFlags.BoolVar(&redacted, "redacted", false, "Whether the flag should be redacted") setFlags.Parse(args) setArgs := setFlags.Args() - if len(setArgs) != 2 { + if redacted { + if len(setArgs) != 1 { + return fmt.Errorf("set command expected '--redacted=true flag', got: --redacted=true %s", strings.Join(setArgs, " ")) + } + } else if len(setArgs) != 2 { return fmt.Errorf("set command expected flag and value, got: %s", strings.Join(setArgs, " ")) } name := setArgs[0] - value := setArgs[1] + if !redacted { + value = setArgs[1] + } release, err := configs.GetReleaseConfig(targetRelease) targetRelease = release.Name if err != nil { @@ -296,8 +305,12 @@ func SetCommand(configs *rc_lib.ReleaseConfigs, commonFlags Flags, cmd string, a } flagValue := &rc_proto.FlagValue{ - Name: proto.String(name), - Value: rc_lib.UnmarshalValue(value), + Name: proto.String(name), + } + if redacted { + flagValue.Redacted = proto.Bool(true) + } else { + flagValue.Value = rc_lib.UnmarshalValue(value) } flagPath := filepath.Join(valueDir, "flag_values", targetRelease, fmt.Sprintf("%s.textproto", name)) err = rc_lib.WriteMessage(flagPath, flagValue) @@ -310,7 +323,7 @@ func SetCommand(configs *rc_lib.ReleaseConfigs, commonFlags Flags, cmd string, a if err != nil { return err } - err = GetCommand(configs, commonFlags, cmd, args[0:1]) + err = GetCommand(configs, commonFlags, cmd, []string{name}) if err != nil { return err }