@@ -2,6 +2,7 @@ package docker
22
33import (
44 "bytes"
5+ "encoding/base64"
56 "encoding/json"
67 "fmt"
78 "os"
@@ -60,36 +61,39 @@ type (
6061
6162 // Build defines Docker build parameters.
6263 Build struct {
63- Remote string // Git remote URL
64- Name string // Docker build using default named tag
65- Dockerfile string // Docker build Dockerfile
66- Context string // Docker build context
67- Tags []string // Docker build tags
68- Args []string // Docker build args
69- ArgsEnv []string // Docker build args from env
70- ArgsNew []string // Docker build args with comma seperated values
71- IsMultipleBuildArgs bool // env variable for fall back
72- Target string // Docker build target
73- Squash bool // Docker build squash
74- Pull bool // Docker build pull
75- CacheFrom []string // Docker buildx cache-from
76- CacheTo []string // Docker buildx cache-to
77- Compress bool // Docker build compress
78- Repo string // Docker build repository
79- LabelSchema []string // label-schema Label map
80- AutoLabel bool // auto-label bool
81- Labels []string // Label map
82- Link string // Git repo link
83- NoCache bool // Docker build no-cache
84- Secret string // secret keypair
85- SecretEnvs []string // Docker build secrets with env var as source
86- SecretFiles []string // Docker build secrets with file as source
87- AddHost []string // Docker build add-host
88- Quiet bool // Docker build quiet
89- Platform string // Docker build platform
90- SSHAgentKey string // Docker build ssh agent key
91- SSHKeyPath string // Docker build ssh key path
92- BuildxLoad bool // Docker buildx --load
64+ Remote string // Git remote URL
65+ Name string // Docker build using default named tag
66+ Dockerfile string // Docker build Dockerfile
67+ Context string // Docker build context
68+ Tags []string // Docker build tags
69+ Args []string // Docker build args
70+ ArgsEnv []string // Docker build args from env
71+ ArgsNew []string // Docker build args with comma seperated values
72+ IsMultipleBuildArgs bool // env variable for fall back
73+ Target string // Docker build target
74+ Squash bool // Docker build squash
75+ Pull bool // Docker build pull
76+ CacheFrom []string // Docker buildx cache-from
77+ CacheTo []string // Docker buildx cache-to
78+ Compress bool // Docker build compress
79+ Repo string // Docker build repository
80+ LabelSchema []string // label-schema Label map
81+ AutoLabel bool // auto-label bool
82+ Labels []string // Label map
83+ Link string // Git repo link
84+ NoCache bool // Docker build no-cache
85+ Secret string // secret keypair
86+ SecretEnvs []string // Docker build secrets with env var as source
87+ SecretFiles []string // Docker build secrets with file as source
88+ AddHost []string // Docker build add-host
89+ Quiet bool // Docker build quiet
90+ Platform string // Docker build platform
91+ SSHAgentKey string // Docker build ssh agent key
92+ SSHKeyPath string // Docker build ssh key path
93+ BuildxLoad bool // Docker buildx --load
94+ HarnessSelfHostedS3AccessKey string // Harness self-hosted s3 access key
95+ HarnessSelfHostedS3SecretKey string // Harness self-hosted s3 secret key
96+ HarnessSelfHostedGcpJsonKey string // Harness self hosted gcp json region
9397 }
9498
9599 // Plugin defines the Docker plugin parameters.
@@ -562,6 +566,8 @@ func commandBuildx(build Build, builder Builder, dryrun bool, metadataFile strin
562566 "-f" , build .Dockerfile ,
563567 }
564568
569+ sanitizeCacheCommand (& build )
570+
565571 if builder .Name != "" {
566572 args = append (args , "--builder" , builder .Name )
567573 }
@@ -664,6 +670,46 @@ func commandBuildx(build Build, builder Builder, dryrun bool, metadataFile strin
664670 return exec .Command (dockerExe , args ... )
665671}
666672
673+ func sanitizeCacheCommand (build * Build ) {
674+ // Helper function to sanitize cache arguments
675+ sanitizeCacheArgs := func (args []string ) []string {
676+ for i , arg := range args {
677+
678+ // Replace access_key_id if placeholder exists and the actual key is not empty
679+ if strings .Contains (arg , "access_key_id=harness_placeholder_aws_creds" ) && build .HarnessSelfHostedS3AccessKey != "" {
680+ arg = strings .Replace (arg , "access_key_id=harness_placeholder_aws_creds" , "access_key_id=" + build .HarnessSelfHostedS3AccessKey , 1 )
681+ }
682+
683+ // Replace secret_access_key if placeholder exists and the actual key is not empty
684+ if strings .Contains (arg , "secret_access_key=harness_placeholder_aws_creds" ) && build .HarnessSelfHostedS3SecretKey != "" {
685+ arg = strings .Replace (arg , "secret_access_key=harness_placeholder_aws_creds" , "secret_access_key=" + build .HarnessSelfHostedS3SecretKey , 1 )
686+ }
687+
688+ // Handle gcp_json_key
689+ if strings .Contains (arg , "gcp_json_key=harness_placeholder_gcp_creds" ) {
690+ if build .HarnessSelfHostedGcpJsonKey != "" {
691+ // Base64 encode the GCP JSON key
692+ encodedGCPJsonKey := base64 .StdEncoding .EncodeToString ([]byte (build .HarnessSelfHostedGcpJsonKey ))
693+ // Replace the placeholder with the base64-encoded GCP JSON key
694+ arg = strings .Replace (arg , "gcp_json_key=harness_placeholder_gcp_creds" , "gcp_json_key=" + encodedGCPJsonKey , 1 )
695+ } else {
696+ // Remove the gcp_json_key substring if the actual key is empty
697+ arg = strings .Replace (arg , ",gcp_json_key=harness_placeholder_gcp_creds" , "" , 1 )
698+ arg = strings .Replace (arg , "gcp_json_key=harness_placeholder_gcp_creds," , "" , 1 )
699+ arg = strings .Replace (arg , "gcp_json_key=harness_placeholder_gcp_creds" , "" , 1 )
700+ }
701+ }
702+
703+ // Update the argument
704+ args [i ] = arg
705+ }
706+ return args
707+ }
708+
709+ build .CacheFrom = sanitizeCacheArgs (build .CacheFrom )
710+ build .CacheTo = sanitizeCacheArgs (build .CacheTo )
711+ }
712+
667713func getSecretStringCmdArg (kvp string ) (string , error ) {
668714 return getSecretCmdArg (kvp , false )
669715}
0 commit comments