-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🧹 Refactor auth opts for registries. Pass through opts when pulling r…
…egistry images (#3738) * 🧹 Refactor auth opts for registries. Pass through options when pulling registry images. Signed-off-by: Preslav <[email protected]> * Add defaults for GetImageDescriptor and LoadImage. --------- Signed-off-by: Preslav <[email protected]>
- Loading branch information
1 parent
46664a5
commit 1e1dc6f
Showing
5 changed files
with
95 additions
and
221 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// Copyright (c) Mondoo, Inc. | ||
// SPDX-License-Identifier: BUSL-1.1 | ||
|
||
package auth | ||
|
||
import ( | ||
"crypto/tls" | ||
"net" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/google/go-containerregistry/pkg/authn" | ||
"github.com/google/go-containerregistry/pkg/v1/remote" | ||
"github.com/rs/zerolog/log" | ||
"go.mondoo.com/cnquery/v10/logger" | ||
"go.mondoo.com/cnquery/v10/providers-sdk/v1/vault" | ||
) | ||
|
||
func TransportOption(insecure bool) remote.Option { | ||
tr := &http.Transport{ | ||
Proxy: http.ProxyFromEnvironment, | ||
DialContext: (&net.Dialer{ | ||
Timeout: 30 * time.Second, | ||
KeepAlive: 30 * time.Second, | ||
DualStack: true, | ||
}).DialContext, | ||
ForceAttemptHTTP2: true, | ||
MaxIdleConns: 100, | ||
IdleConnTimeout: 90 * time.Second, | ||
TLSHandshakeTimeout: 10 * time.Second, | ||
ExpectContinueTimeout: 1 * time.Second, | ||
} | ||
|
||
if insecure { | ||
tr.TLSClientConfig = &tls.Config{ | ||
InsecureSkipVerify: true, | ||
} | ||
} | ||
return remote.WithTransport(tr) | ||
} | ||
|
||
func AuthOption(ref string, credentials []*vault.Credential) remote.Option { | ||
for i := range credentials { | ||
cred := credentials[i] | ||
switch cred.Type { | ||
case vault.CredentialType_password: | ||
log.Debug().Msg("add password authentication") | ||
cfg := authn.AuthConfig{ | ||
Username: cred.User, | ||
Password: string(cred.Secret), | ||
} | ||
return remote.WithAuth((authn.FromConfig(cfg))) | ||
case vault.CredentialType_bearer: | ||
log.Debug().Str("token", string(cred.Secret)).Msg("add bearer authentication") | ||
cfg := authn.AuthConfig{ | ||
Username: cred.User, | ||
RegistryToken: string(cred.Secret), | ||
} | ||
return remote.WithAuth((authn.FromConfig(cfg))) | ||
default: | ||
log.Warn().Msg("unknown credentials for container image") | ||
logger.DebugJSON(credentials) | ||
} | ||
} | ||
log.Debug().Msg("no credentials for container image, falling back to default auth") | ||
return remote.WithAuthFromKeychain(ConstructKeychain(ref)) | ||
} | ||
|
||
func DefaultOpts(ref string, insecure bool) []remote.Option { | ||
return []remote.Option{AuthOption(ref, nil), TransportOption(insecure)} | ||
} |
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
Oops, something went wrong.