11package docker
22
33import (
4+ "encoding/base64"
45 "encoding/json"
56 "fmt"
7+ "log"
68 "os"
79 "os/exec"
810 "path/filepath"
@@ -53,34 +55,36 @@ type (
5355
5456 // Build defines Docker build parameters.
5557 Build struct {
56- Remote string // Git remote URL
57- Name string // Docker build using default named tag
58- Dockerfile string // Docker build Dockerfile
59- Context string // Docker build context
60- Tags []string // Docker build tags
61- Args []string // Docker build args
62- ArgsEnv []string // Docker build args from env
63- Target string // Docker build target
64- Squash bool // Docker build squash
65- Pull bool // Docker build pull
66- CacheFrom []string // Docker buildx cache-from
67- CacheTo []string // Docker buildx cache-to
68- Compress bool // Docker build compress
69- Repo string // Docker build repository
70- LabelSchema []string // label-schema Label map
71- AutoLabel bool // auto-label bool
72- Labels []string // Label map
73- Link string // Git repo link
74- NoCache bool // Docker build no-cache
75- Secret string // secret keypair
76- SecretEnvs []string // Docker build secrets with env var as source
77- SecretFiles []string // Docker build secrets with file as source
78- AddHost []string // Docker build add-host
79- Quiet bool // Docker build quiet
80- Platform string // Docker build platform
81- SSHAgentKey string // Docker build ssh agent key
82- SSHKeyPath string // Docker build ssh key path
83- BuildxLoad bool // Docker buildx --load
58+ Remote string // Git remote URL
59+ Name string // Docker build using default named tag
60+ Dockerfile string // Docker build Dockerfile
61+ Context string // Docker build context
62+ Tags []string // Docker build tags
63+ Args []string // Docker build args
64+ ArgsEnv []string // Docker build args from env
65+ Target string // Docker build target
66+ Squash bool // Docker build squash
67+ Pull bool // Docker build pull
68+ CacheFrom []string // Docker buildx cache-from
69+ CacheTo []string // Docker buildx cache-to
70+ Compress bool // Docker build compress
71+ Repo string // Docker build repository
72+ LabelSchema []string // label-schema Label map
73+ AutoLabel bool // auto-label bool
74+ Labels []string // Label map
75+ Link string // Git repo link
76+ NoCache bool // Docker build no-cache
77+ Secret string // secret keypair
78+ SecretEnvs []string // Docker build secrets with env var as source
79+ SecretFiles []string // Docker build secrets with file as source
80+ AddHost []string // Docker build add-host
81+ Quiet bool // Docker build quiet
82+ Platform string // Docker build platform
83+ SSHAgentKey string // Docker build ssh agent key
84+ SSHKeyPath string // Docker build ssh key path
85+ BuildxLoad bool // Docker buildx --load
86+ DecodeEnvSecret bool // Decode the secret value in env
87+ EncodedSecretEnvs []string // Docker build env secrets that are encoded using base64
8488 }
8589
8690 // Plugin defines the Docker plugin parameters.
@@ -150,7 +154,6 @@ func (p Plugin) Exec() error {
150154 }
151155 time .Sleep (time .Second * 1 )
152156 }
153-
154157 // for debugging purposes, log the type of authentication
155158 // credentials that have been provided.
156159 switch {
@@ -165,7 +168,6 @@ func (p Plugin) Exec() error {
165168 default :
166169 fmt .Println ("Registry credentials or Docker config not provided. Guest mode enabled." )
167170 }
168-
169171 // create Auth Config File
170172 if p .Login .Config != "" {
171173 os .MkdirAll (dockerHome , 0600 )
@@ -467,6 +469,30 @@ func commandInfo() *exec.Cmd {
467469 return exec .Command (dockerExe , "info" )
468470}
469471
472+ // helper function to update env var value from base64 encoded to decoded
473+ func updateEnvWithDecodedValue (encodedEnvList []string ) error {
474+ for _ , envName := range encodedEnvList {
475+ // Get the current base64 encoded value
476+ encodedValue := os .Getenv (envName )
477+ if encodedValue == "" {
478+ return fmt .Errorf ("environment variable %s not found" , envName )
479+ }
480+
481+ // Decode the base64 value
482+ decodedBytes , err := base64 .StdEncoding .DecodeString (encodedValue )
483+ if err != nil {
484+ return fmt .Errorf ("failed to decode value for %s: %v" , envName , err )
485+ }
486+
487+ // Update the environment variable with the decoded value
488+ err = os .Setenv (envName , string (decodedBytes ))
489+ if err != nil {
490+ return fmt .Errorf ("failed to set environment variable %s: %v" , envName , err )
491+ }
492+ }
493+ return nil
494+ }
495+
470496// helper function to create the docker buildx command.
471497func commandBuildx (build Build , builder Builder , dryrun bool , metadataFile string ) * exec.Cmd {
472498 args := []string {
@@ -523,6 +549,13 @@ func commandBuildx(build Build, builder Builder, dryrun bool, metadataFile strin
523549 if build .Secret != "" {
524550 args = append (args , "--secret" , build .Secret )
525551 }
552+ // update the list of env variables that have been encoded with base64
553+ if build .DecodeEnvSecret {
554+ err := updateEnvWithDecodedValue (build .EncodedSecretEnvs )
555+ if err != nil {
556+ log .Printf ("failed to decode harness secrets used as docker secrets in the build command: %v" , err )
557+ }
558+ }
526559 for _ , secret := range build .SecretEnvs {
527560 if arg , err := getSecretStringCmdArg (secret ); err == nil {
528561 args = append (args , "--secret" , arg )
0 commit comments