-
Notifications
You must be signed in to change notification settings - Fork 782
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for env var secret sources #3598
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -415,7 +415,7 @@ func runSetupBuiltinVolumes(mountLabel, mountPoint, containerDir string, builtin | |
return mounts, nil | ||
} | ||
|
||
func (b *Builder) setupMounts(mountPoint string, spec *specs.Spec, bundlePath string, optionMounts []specs.Mount, bindFiles map[string]string, builtinVolumes, volumeMounts []string, shmSize string, namespaceOptions define.NamespaceOptions, secrets map[string]string, sshSources map[string]*sshagent.Source, runFileMounts []string, contextDir string) (*runMountArtifacts, error) { | ||
func (b *Builder) setupMounts(mountPoint string, spec *specs.Spec, bundlePath string, optionMounts []specs.Mount, bindFiles map[string]string, builtinVolumes, volumeMounts []string, shmSize string, namespaceOptions define.NamespaceOptions, secrets map[string]define.Secret, sshSources map[string]*sshagent.Source, runFileMounts []string, contextDir string) (*runMountArtifacts, error) { | ||
// Start building a new list of mounts. | ||
var mounts []specs.Mount | ||
haveMount := func(destination string) bool { | ||
|
@@ -2366,8 +2366,9 @@ func init() { | |
} | ||
|
||
// runSetupRunMounts sets up mounts that exist only in this RUN, not in subsequent runs | ||
func (b *Builder) runSetupRunMounts(mounts []string, secrets map[string]string, sshSources map[string]*sshagent.Source, containerWorkingDir string, contextDir string, uidmap []spec.LinuxIDMapping, gidmap []spec.LinuxIDMapping, rootUID int, rootGID int, processUID int, processGID int) ([]spec.Mount, *runMountArtifacts, error) { | ||
mountTargets := make([]string, 0, 10) | ||
func (b *Builder) runSetupRunMounts(mounts []string, secrets map[string]define.Secret, sshSources map[string]*sshagent.Source, containerWorkingDir string, contextDir string, uidmap []spec.LinuxIDMapping, gidmap []spec.LinuxIDMapping, rootUID int, rootGID int, processUID int, processGID int) ([]spec.Mount, *runMountArtifacts, error) { | ||
mountTargets := make([]string, 0, len(mounts)) | ||
tmpFiles := make([]string, 0, len(mounts)) | ||
finalMounts := make([]specs.Mount, 0, len(mounts)) | ||
agents := make([]*sshagent.AgentServer, 0, len(mounts)) | ||
sshCount := 0 | ||
|
@@ -2386,14 +2387,16 @@ func (b *Builder) runSetupRunMounts(mounts []string, secrets map[string]string, | |
// For now, we only support type secret. | ||
switch kv[1] { | ||
case "secret": | ||
mount, err := getSecretMount(tokens, secrets, b.MountLabel, containerWorkingDir, uidmap, gidmap) | ||
mount, envFile, err := getSecretMount(tokens, secrets, b.MountLabel, containerWorkingDir, uidmap, gidmap) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
if mount != nil { | ||
finalMounts = append(finalMounts, *mount) | ||
mountTargets = append(mountTargets, mount.Destination) | ||
|
||
if envFile != "" { | ||
tmpFiles = append(tmpFiles, envFile) | ||
} | ||
} | ||
case "ssh": | ||
mount, agent, err := b.getSSHMount(tokens, sshCount, sshSources, b.MountLabel, uidmap, gidmap, b.ProcessLabel) | ||
|
@@ -2437,6 +2440,7 @@ func (b *Builder) runSetupRunMounts(mounts []string, secrets map[string]string, | |
} | ||
artifacts := &runMountArtifacts{ | ||
RunMountTargets: mountTargets, | ||
TmpFiles: tmpFiles, | ||
Agents: agents, | ||
SSHAuthSock: defaultSSHSock, | ||
} | ||
|
@@ -2488,10 +2492,10 @@ func (b *Builder) getCacheMount(tokens []string, rootUID, rootGID, processUID, p | |
return &volumes[0], nil | ||
} | ||
|
||
func getSecretMount(tokens []string, secrets map[string]string, mountlabel string, containerWorkingDir string, uidmap []spec.LinuxIDMapping, gidmap []spec.LinuxIDMapping) (*spec.Mount, error) { | ||
func getSecretMount(tokens []string, secrets map[string]define.Secret, mountlabel string, containerWorkingDir string, uidmap []spec.LinuxIDMapping, gidmap []spec.LinuxIDMapping) (*spec.Mount, string, error) { | ||
errInvalidSyntax := errors.New("secret should have syntax id=id[,target=path,required=bool,mode=uint,uid=uint,gid=uint") | ||
if len(tokens) == 0 { | ||
return nil, errInvalidSyntax | ||
return nil, "", errInvalidSyntax | ||
} | ||
var err error | ||
var id, target string | ||
|
@@ -2508,84 +2512,102 @@ func getSecretMount(tokens []string, secrets map[string]string, mountlabel strin | |
case "required": | ||
required, err = strconv.ParseBool(kv[1]) | ||
if err != nil { | ||
return nil, errInvalidSyntax | ||
return nil, "", errInvalidSyntax | ||
} | ||
case "mode": | ||
mode64, err := strconv.ParseUint(kv[1], 8, 32) | ||
if err != nil { | ||
return nil, errInvalidSyntax | ||
return nil, "", errInvalidSyntax | ||
} | ||
mode = uint32(mode64) | ||
case "uid": | ||
uid64, err := strconv.ParseUint(kv[1], 10, 32) | ||
if err != nil { | ||
return nil, errInvalidSyntax | ||
return nil, "", errInvalidSyntax | ||
} | ||
uid = uint32(uid64) | ||
case "gid": | ||
gid64, err := strconv.ParseUint(kv[1], 10, 32) | ||
if err != nil { | ||
return nil, errInvalidSyntax | ||
return nil, "", errInvalidSyntax | ||
} | ||
gid = uint32(gid64) | ||
default: | ||
return nil, errInvalidSyntax | ||
return nil, "", errInvalidSyntax | ||
} | ||
} | ||
|
||
if id == "" { | ||
return nil, errInvalidSyntax | ||
return nil, "", errInvalidSyntax | ||
} | ||
// Default location for secretis is /run/secrets/id | ||
if target == "" { | ||
target = "/run/secrets/" + id | ||
} | ||
|
||
src, ok := secrets[id] | ||
secr, ok := secrets[id] | ||
if !ok { | ||
if required { | ||
return nil, errors.Errorf("secret required but no secret with id %s found", id) | ||
return nil, "", errors.Errorf("secret required but no secret with id %s found", id) | ||
} | ||
return nil, nil | ||
return nil, "", nil | ||
} | ||
var data []byte | ||
var envFile string | ||
var ctrFileOnHost string | ||
|
||
// Copy secrets to container working dir, since we need to chmod, chown and relabel it | ||
// for the container user and we don't want to mess with the original file | ||
ctrFileOnHost := filepath.Join(containerWorkingDir, "secrets", id) | ||
_, err = os.Stat(ctrFileOnHost) | ||
if os.IsNotExist(err) { | ||
data, err := ioutil.ReadFile(src) | ||
switch secr.SourceType { | ||
case "env": | ||
data = []byte(os.Getenv(secr.Source)) | ||
tmpFile, err := ioutil.TempFile("/dev/shm", "buildah*") | ||
if err != nil { | ||
return nil, err | ||
return nil, "", err | ||
} | ||
if err := os.MkdirAll(filepath.Dir(ctrFileOnHost), 0644); err != nil { | ||
return nil, err | ||
envFile = tmpFile.Name() | ||
ctrFileOnHost = tmpFile.Name() | ||
case "file": | ||
data, err = ioutil.ReadFile(secr.Source) | ||
if err != nil { | ||
return nil, "", err | ||
} | ||
if err := ioutil.WriteFile(ctrFileOnHost, data, 0644); err != nil { | ||
return nil, err | ||
ctrFileOnHost = filepath.Join(containerWorkingDir, "secrets", id) | ||
_, err = os.Stat(ctrFileOnHost) | ||
if !os.IsNotExist(err) { | ||
return nil, "", err | ||
} | ||
default: | ||
return nil, "", errors.New("invalid source secret type") | ||
} | ||
|
||
// Copy secrets to container working dir (or tmp dir if it's an env), since we need to chmod, | ||
// chown and relabel it for the container user and we don't want to mess with the original file | ||
if err := os.MkdirAll(filepath.Dir(ctrFileOnHost), 0644); err != nil { | ||
return nil, "", err | ||
} | ||
if err := ioutil.WriteFile(ctrFileOnHost, data, 0644); err != nil { | ||
return nil, "", err | ||
} | ||
|
||
if err := label.Relabel(ctrFileOnHost, mountlabel, false); err != nil { | ||
return nil, err | ||
return nil, "", err | ||
} | ||
hostUID, hostGID, err := util.GetHostIDs(uidmap, gidmap, uid, gid) | ||
if err != nil { | ||
return nil, err | ||
return nil, "", err | ||
} | ||
if err := os.Lchown(ctrFileOnHost, int(hostUID), int(hostGID)); err != nil { | ||
return nil, err | ||
return nil, "", err | ||
} | ||
if err := os.Chmod(ctrFileOnHost, os.FileMode(mode)); err != nil { | ||
return nil, err | ||
return nil, "", err | ||
} | ||
newMount := specs.Mount{ | ||
Destination: target, | ||
Type: "bind", | ||
Source: ctrFileOnHost, | ||
Options: []string{"bind", "rprivate", "ro"}, | ||
} | ||
return &newMount, nil | ||
return &newMount, envFile, nil | ||
} | ||
|
||
// getSSHMount parses the --mount type=ssh flag in the Containerfile, checks if there's an ssh source provided, and creates and starts an ssh-agent to be forwarded into the container | ||
|
@@ -2721,5 +2743,15 @@ func cleanupRunMounts(mountpoint string, artifacts *runMountArtifacts) error { | |
return err | ||
} | ||
} | ||
return nil | ||
var prevErr error | ||
for _, path := range artifacts.TmpFiles { | ||
err := os.Remove(path) | ||
if !os.IsNotExist(err) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you save the err in a prevErr field and only return after you delete all TmpFiles.
|
||
if prevErr != nil { | ||
logrus.Error(prevErr) | ||
} | ||
prevErr = err | ||
} | ||
} | ||
return prevErr | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Make sure the buildah-* files are not world readable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would this just be a chmod or something else?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
TempFile()
implementation specifies permissions of0600
when creating the file, so that's fine.