implemented integration tests for engine.#Pull + moved auth code separately for sharing code with other tasks later
Signed-off-by: Sam Alba <samalba@users.noreply.github.com>
This commit is contained in:
54
plan/task/auth.go
Normal file
54
plan/task/auth.go
Normal file
@@ -0,0 +1,54 @@
|
||||
package task
|
||||
|
||||
import (
|
||||
"go.dagger.io/dagger/compiler"
|
||||
"go.dagger.io/dagger/plancontext"
|
||||
)
|
||||
|
||||
type authValue struct {
|
||||
Target string
|
||||
Username string
|
||||
Secret *plancontext.Secret
|
||||
}
|
||||
|
||||
// Decodes an auth field value
|
||||
//
|
||||
// Cue format:
|
||||
// auth: [...{
|
||||
// target: string
|
||||
// username: string
|
||||
// secret: string | #Secret
|
||||
// }]
|
||||
func decodeAuthValue(pctx *plancontext.Context, v *compiler.Value) ([]*authValue, error) {
|
||||
vals, err := v.List()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
authVals := []*authValue{}
|
||||
for _, val := range vals {
|
||||
authVal := authValue{}
|
||||
|
||||
target, err := val.Lookup("target").String()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
authVal.Target = target
|
||||
|
||||
username, err := val.Lookup("username").String()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
authVal.Username = username
|
||||
|
||||
secret, err := pctx.Secrets.FromValue(val.Lookup("secret"))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
authVal.Secret = secret
|
||||
|
||||
authVals = append(authVals, &authVal)
|
||||
}
|
||||
|
||||
return authVals, nil
|
||||
}
|
@@ -7,6 +7,7 @@ import (
|
||||
|
||||
"github.com/docker/distribution/reference"
|
||||
"github.com/moby/buildkit/client/llb"
|
||||
"github.com/rs/zerolog/log"
|
||||
"go.dagger.io/dagger/compiler"
|
||||
"go.dagger.io/dagger/plancontext"
|
||||
"go.dagger.io/dagger/solver"
|
||||
@@ -19,20 +20,24 @@ func init() {
|
||||
type pullTask struct {
|
||||
}
|
||||
|
||||
type authValue struct {
|
||||
Target string
|
||||
Username string
|
||||
// FIXME: handle secrets
|
||||
Secret string
|
||||
}
|
||||
|
||||
func (c *pullTask) Run(ctx context.Context, pctx *plancontext.Context, s solver.Solver, v *compiler.Value) (*compiler.Value, error) {
|
||||
// FIXME: handle auth
|
||||
lg := log.Ctx(ctx)
|
||||
|
||||
rawRef, err := v.Lookup("source").String()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Read auth info
|
||||
auth, err := decodeAuthValue(pctx, v.Lookup("auth"))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, a := range auth {
|
||||
s.AddCredentials(a.Target, a.Username, a.Secret.PlainText())
|
||||
lg.Debug().Str("target", a.Target).Msg("add target credentials")
|
||||
}
|
||||
|
||||
ref, err := reference.ParseNormalizedNamed(rawRef)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to parse ref %s: %w", rawRef, err)
|
||||
@@ -54,6 +59,7 @@ func (c *pullTask) Run(ctx context.Context, pctx *plancontext.Context, s solver.
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
imageJSON, err := json.Marshal(image)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -64,17 +70,6 @@ func (c *pullTask) Run(ctx context.Context, pctx *plancontext.Context, s solver.
|
||||
return nil, err
|
||||
}
|
||||
|
||||
auth := []authValue{}
|
||||
|
||||
// Read auth data
|
||||
if err := v.Lookup("auth").Decode(&auth); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, a := range auth {
|
||||
s.AddCredentials(a.Target, a.Username, a.Secret)
|
||||
}
|
||||
|
||||
result, err := s.Solve(ctx, st, pctx.Platform.Get())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
Reference in New Issue
Block a user