Skip to content

Commit

Permalink
feat: Support Private Docker Registries (#248)
Browse files Browse the repository at this point in the history

<!--
Explain what problem this PR addresses
-->

---
  • Loading branch information
bbernays authored Feb 23, 2024
1 parent a6de6db commit 09ca0ec
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
4 changes: 3 additions & 1 deletion managedplugin/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func isDockerImageAvailable(ctx context.Context, imageName string) (bool, error)
return len(images) > 0, nil
}

func pullDockerImage(ctx context.Context, imageName string, authToken string, teamName string) error {
func pullDockerImage(ctx context.Context, imageName string, authToken string, teamName string, dockerHubAuth string) error {
// Pull the image
additionalHeaders := make(map[string]string)
opts := types.ImagePullOptions{}
Expand Down Expand Up @@ -119,6 +119,8 @@ func pullDockerImage(ctx context.Context, imageName string, authToken string, te
return fmt.Errorf("failed to encode Docker auth config: %v", err)
}
opts.RegistryAuth = encodedAuth
} else if dockerHubAuth != "" {
opts.RegistryAuth = dockerHubAuth
}

cli, err := client.NewClientWithOpts(client.FromEnv, client.WithHTTPHeaders(additionalHeaders))
Expand Down
2 changes: 1 addition & 1 deletion managedplugin/hub.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

func getHubClient(logger zerolog.Logger, ops HubDownloadOptions) (*cloudquery_api.ClientWithResponses, error) {
c, err := cloudquery_api.NewClientWithResponses(APIBaseURL(),
cloudquery_api.WithRequestEditorFn(func(ctx context.Context, req *http.Request) error {
cloudquery_api.WithRequestEditorFn(func(_ context.Context, req *http.Request) error {
if ops.AuthToken != "" {
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", ops.AuthToken))
}
Expand Down
8 changes: 6 additions & 2 deletions managedplugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ type Config struct {
Path string
Version string
Environment []string // environment variables to pass to the plugin in key=value format
DockerAuth string
}

type Client struct {
Expand All @@ -98,6 +99,7 @@ type Client struct {
authToken string
teamName string
licenseFile string
dockerAuth string
}

// typ will be deprecated soon but now required for a transition period
Expand Down Expand Up @@ -144,6 +146,7 @@ func NewClient(ctx context.Context, typ PluginType, config Config, opts ...Optio
metrics: &Metrics{},
registry: config.Registry,
cqDockerHost: DefaultCloudQueryDockerHost,
dockerAuth: config.DockerAuth,
}
for _, opt := range opts {
opt(c)
Expand Down Expand Up @@ -179,7 +182,7 @@ func (c *Client) downloadPlugin(ctx context.Context, typ PluginType) error {
if imageAvailable, err := isDockerImageAvailable(ctx, c.config.Path); err != nil {
return err
} else if !imageAvailable {
return pullDockerImage(ctx, c.config.Path, c.authToken, c.teamName)
return pullDockerImage(ctx, c.config.Path, c.authToken, c.teamName, c.dockerAuth)
}
return nil
case RegistryCloudQuery:
Expand Down Expand Up @@ -215,7 +218,7 @@ func (c *Client) downloadPlugin(ctx context.Context, typ PluginType) error {
if imageAvailable, err := isDockerImageAvailable(ctx, path); err != nil {
return err
} else if !imageAvailable {
return pullDockerImage(ctx, path, c.authToken, c.teamName)
return pullDockerImage(ctx, path, c.authToken, c.teamName, "")
}
return nil
}
Expand Down Expand Up @@ -269,6 +272,7 @@ func (c *Client) startDockerPlugin(ctx context.Context, configPath string) error
if err != nil {
return fmt.Errorf("failed to create Docker client: %w", err)
}
cli.NegotiateAPIVersion(ctx)
pluginArgs := c.getPluginArgs()
config := &container.Config{
ExposedPorts: nat.PortSet{
Expand Down

0 comments on commit 09ca0ec

Please sign in to comment.