-
Notifications
You must be signed in to change notification settings - Fork 97
feat: support auth from docker config #2560
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
Merged
Merged
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
13a5852
feat: support auth from docker config
skylenet a6905b7
try pass linter checks
skylenet 961d487
add tests
skylenet 41706c0
cleanup spew
skylenet 7b02962
linter warnings
skylenet 2e53e92
linter fixup
skylenet 41451af
try fix gitguardian complain
skylenet 181e6c9
add missing auth
skylenet 7e2b88c
add example test for pass
skylenet c8254bb
linter happiness
skylenet c63cf52
generate docker config file for engine container
skylenet 04c515d
Merge branch 'main' of github.com:kurtosis-tech/kurtosis into skylene…
skylenet 2b582d4
add docker config volume to API container
skylenet 4649880
cleanup comments
skylenet 3ab25bb
more explicit var name
skylenet 922f5aa
use const for DOCKER_CONFIG env var
skylenet 26b26e9
use stacktrace.Propagate instead of fmt.Errorf
skylenet e4ab668
Merge branch 'main' of github.com:kurtosis-tech/kurtosis into skylene…
skylenet 1b83250
use cmd string for err
tedim52 31ecf0f
return empty auth config if no config.json found
tedim52 4f923a5
add debug log
tedim52 f63af84
adjust enclave continuity ci test
tedim52 fad66b8
adjust basic cli ci test
tedim52 7912ab8
trigger ci
tedim52 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
175 changes: 175 additions & 0 deletions
175
...s_backend/engine_functions/docker_config_storage_creator/docker_config_storage_creator.go
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,175 @@ | ||
| package docker_config_storage_creator | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "context" | ||
| "encoding/json" | ||
| "fmt" | ||
| "time" | ||
|
|
||
| "github.com/docker/docker/api/types/registry" | ||
| "github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_impls/docker/docker_manager" | ||
| "github.com/kurtosis-tech/stacktrace" | ||
| "github.com/sirupsen/logrus" | ||
| ) | ||
|
|
||
| const ( | ||
| // We use this image and version because we already are using this in other projects so there is a high probability | ||
| // that the image is in the local machine's cache | ||
| creatorContainerImage = "alpine:3.17" | ||
| creatorContainerName = "kurtosis-docker-config-storage-creator" | ||
|
|
||
| shBinaryFilepath = "/bin/sh" | ||
| shCmdFlag = "-c" | ||
| printfCmdName = "printf" | ||
|
|
||
| creationSuccessExitCode = 0 | ||
|
|
||
| creationCmdMaxRetries = 2 | ||
| creationCmdDelayInRetries = 200 * time.Millisecond | ||
|
|
||
| configFilePath = "config.json" | ||
|
|
||
| sleepSeconds = 1800 | ||
| ) | ||
|
|
||
| func CreateDockerConfigStorage( | ||
| ctx context.Context, | ||
| targetNetworkId string, | ||
| volumeName string, | ||
| storageDirPath string, | ||
| dockerManager *docker_manager.DockerManager, | ||
| ) error { | ||
| entrypointArgs := []string{ | ||
| shBinaryFilepath, | ||
| shCmdFlag, | ||
| fmt.Sprintf("sleep %v", sleepSeconds), | ||
| } | ||
|
|
||
| volumeMounts := map[string]string{ | ||
| volumeName: storageDirPath, | ||
| } | ||
|
|
||
| createAndStartArgs := docker_manager.NewCreateAndStartContainerArgsBuilder( | ||
| creatorContainerImage, | ||
| creatorContainerName, | ||
| targetNetworkId, | ||
| ).WithEntrypointArgs( | ||
| entrypointArgs, | ||
| ).WithVolumeMounts( | ||
| volumeMounts, | ||
| ).Build() | ||
|
|
||
| containerId, _, err := dockerManager.CreateAndStartContainer(ctx, createAndStartArgs) | ||
| if err != nil { | ||
| return stacktrace.Propagate(err, "An error occurred starting the Docker Config Storage Creator container with these args '%+v'", createAndStartArgs) | ||
| } | ||
| //The killing step has to be executed always in the success and also in the failed case | ||
| defer func() { | ||
| if err = dockerManager.RemoveContainer(context.Background(), containerId); err != nil { | ||
| logrus.Errorf( | ||
| "Launching the Docker Config Creator container with container ID '%v' didn't complete successfully so we "+ | ||
| "tried to remove the container we started, but doing so exited with an error:\n%v", | ||
| containerId, | ||
| err) | ||
| logrus.Errorf("ACTION REQUIRED: You'll need to manually remove the container with ID '%v'!!!!!!", containerId) | ||
| } | ||
| }() | ||
|
|
||
| if err := storeConfigInVolume( | ||
| ctx, | ||
| dockerManager, | ||
| containerId, | ||
| creationCmdMaxRetries, | ||
| creationCmdDelayInRetries, | ||
| storageDirPath, | ||
| ); err != nil { | ||
| return stacktrace.Propagate(err, "An error occurred creating Docker config storage in volume.") | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func storeConfigInVolume( | ||
| ctx context.Context, | ||
| dockerManager *docker_manager.DockerManager, | ||
| containerId string, | ||
| maxRetries uint, | ||
| timeBetweenRetries time.Duration, | ||
| storageDirPath string, | ||
| ) error { | ||
| // Get all the registries from the Docker config | ||
| registries, err := docker_manager.GetAllRegistriesFromDockerConfig() | ||
| if err != nil { | ||
| return stacktrace.NewError("An error occurred getting all registries from Docker config: %v", err) | ||
| } | ||
|
|
||
| cfg := struct { | ||
| Auths map[string]registry.AuthConfig `json:"auths"` | ||
| }{ | ||
| Auths: make(map[string]registry.AuthConfig), | ||
| } | ||
|
|
||
| // Add the auths for each registry | ||
| for _, registry := range registries { | ||
| creds, err := docker_manager.GetAuthFromDockerConfig(registry) | ||
| if err != nil { | ||
| return stacktrace.NewError("An error occurred getting auth for registry '%v' from Docker config: %v", registry, err) | ||
| } | ||
| cfg.Auths[registry] = *creds | ||
| } | ||
|
|
||
| cfgJsonStr, err := json.Marshal(cfg) | ||
| if err != nil { | ||
| return stacktrace.NewError("An error occurred marshalling the Docker config into JSON: %v", err) | ||
| } | ||
|
|
||
| // Write the config.json to the volume | ||
| commandStr := fmt.Sprintf( | ||
| "%v '%v' > %v", | ||
| printfCmdName, | ||
| string(cfgJsonStr), | ||
| fmt.Sprintf("%s/%s", storageDirPath, configFilePath), | ||
| ) | ||
|
|
||
| execCmd := []string{ | ||
| shBinaryFilepath, | ||
| shCmdFlag, | ||
| commandStr, | ||
| } | ||
| for i := uint(0); i < maxRetries; i++ { | ||
| outputBuffer := &bytes.Buffer{} | ||
| exitCode, err := dockerManager.RunExecCommand(ctx, containerId, execCmd, outputBuffer) | ||
| if err == nil { | ||
| if exitCode == creationSuccessExitCode { | ||
| logrus.Debugf("The Docker config file was successfully added into the volume.") | ||
| return nil | ||
| } | ||
| logrus.Debugf( | ||
| "Docker config storage creation command '%v' returned without a Docker error, but exited with non-%v exit code '%v' and logs:\n%v", | ||
| commandStr, | ||
| creationSuccessExitCode, | ||
| exitCode, | ||
| outputBuffer.String(), | ||
| ) | ||
| } else { | ||
| logrus.Debugf( | ||
| "Docker config storage creation command '%v' experienced a Docker error:\n%v", | ||
| commandStr, | ||
| err, | ||
| ) | ||
| } | ||
|
|
||
| // Tiny optimization to not sleep if we're not going to run the loop again | ||
| if i < maxRetries { | ||
| time.Sleep(timeBetweenRetries) | ||
| } | ||
| } | ||
|
|
||
| return stacktrace.NewError( | ||
| "The Docker config storage creation didn't return success (as measured by the command '%v') even after retrying %v times with %v between retries", | ||
| commandStr, | ||
| maxRetries, | ||
| timeBetweenRetries, | ||
| ) | ||
| } | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.