Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 17 additions & 82 deletions cmd/dagger/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,19 @@ package main
import (
"bytes"
"context"
"crypto/sha256"
"encoding/csv"
"encoding/hex"
"fmt"
"io"
"net"
"net/url"
"os"
"os/exec"
"path/filepath"
"reflect"
"runtime"
"strconv"
"strings"

"github.com/containerd/platforms"
"github.com/dagger/dagger/engine/client"
"github.com/dagger/dagger/engine/client/pathutil"
"github.com/moby/buildkit/util/gitutil"
"github.com/spf13/pflag"

Expand Down Expand Up @@ -345,7 +341,7 @@ func (v *directoryValue) Get(ctx context.Context, dag *dagger.Client, modSrc *da
if err != nil {
return nil, err
}
path, err = client.ExpandHomeDir(homeDir, path)
path, err = pathutil.ExpandHomeDir(homeDir, path)
if err != nil {
return nil, fmt.Errorf("failed to expand home directory: %w", err)
}
Expand Down Expand Up @@ -451,11 +447,11 @@ func (v *fileValue) Get(_ context.Context, dag *dagger.Client, _ *dagger.ModuleS
if err != nil {
return nil, err
}
vStr, err = client.ExpandHomeDir(homeDir, vStr)
vStr, err = pathutil.ExpandHomeDir(homeDir, vStr)
if err != nil {
return nil, fmt.Errorf("failed to expand home directory: %w", err)
}
vStr, err = client.Abs(vStr)
vStr, err = pathutil.Abs(vStr)
if err != nil {
return nil, fmt.Errorf("failed to resolve absolute path: %w", err)
}
Expand All @@ -468,96 +464,35 @@ func (v *fileValue) Get(_ context.Context, dag *dagger.Client, _ *dagger.ModuleS
// secretValue is a pflag.Value that builds a dagger.Secret from a name and a
// plaintext value.
type secretValue struct {
secretSource string
sourceVal string
uri string
}

const (
envSecretSource = "env"
fileSecretSource = "file"
commandSecretSource = "cmd"
)

func (v *secretValue) Type() string {
return Secret
}

func (v *secretValue) Set(s string) error {
secretSource, val, ok := strings.Cut(s, ":")
if !ok {
// case of e.g. `--token MY_ENV_SECRET`, which is shorthand for `--token env:MY_ENV_SECRET`
val = secretSource
secretSource = envSecretSource
if !strings.Contains(s, ":") {
// case of e.g. `--token MY_ENV_SECRET`, which is shorthand for `--token env://MY_ENV_SECRET`
s = "env://" + s
}
// legacy secrets in the form of `--token env:MY_ENV_SECRET` instead of `env://MY_ENV_SECRET`
secretSource, val, _ := strings.Cut(s, ":")
if !strings.HasPrefix(val, "//") {
s = secretSource + "://" + val
}
v.secretSource = secretSource
v.sourceVal = val

v.uri = s

return nil
}

func (v *secretValue) String() string {
if v.sourceVal == "" {
return ""
}
return fmt.Sprintf("%s:%s", v.secretSource, v.sourceVal)
return v.uri
}

func (v *secretValue) Get(ctx context.Context, c *dagger.Client, _ *dagger.ModuleSource, _ *modFunctionArg) (any, error) {
var plaintext string

switch v.secretSource {
case envSecretSource:
envPlaintext, ok := os.LookupEnv(v.sourceVal)
if !ok {
// Don't show the entire env var name, in case the user accidentally passed the value instead...
// This is important because users originally *did* have to pass the value, before we changed to
// passing by name instead.
key := v.sourceVal
if len(key) >= 4 {
key = key[:3] + "..."
}
return nil, fmt.Errorf("secret env var not found: %q", key)
}
plaintext = envPlaintext

case fileSecretSource:
homeDir, err := os.UserHomeDir()
if err != nil {
return nil, err
}
sourceVal, err := client.ExpandHomeDir(homeDir, v.sourceVal)
if err != nil {
return nil, err
}
filePlaintext, err := os.ReadFile(sourceVal)
if err != nil {
return nil, fmt.Errorf("failed to read secret file %q: %w", v.sourceVal, err)
}
plaintext = string(filePlaintext)

case commandSecretSource:
var stdoutBytes []byte
var err error
if runtime.GOOS == "windows" {
stdoutBytes, err = exec.CommandContext(ctx, "cmd.exe", "/C", v.sourceVal).Output()
} else {
// #nosec G204
stdoutBytes, err = exec.CommandContext(ctx, "sh", "-c", v.sourceVal).Output()
}
if err != nil {
return nil, fmt.Errorf("failed to run secret command %q: %w", v.sourceVal, err)
}
plaintext = string(stdoutBytes)

default:
return nil, fmt.Errorf("unsupported secret arg source: %q", v.secretSource)
}

// NB: If we allow getting the name from the dagger.Secret instance,
// it can be vulnerable to brute force attacks.
hash := sha256.Sum256([]byte(plaintext))
secretName := hex.EncodeToString(hash[:])
return c.SetSecret(secretName, plaintext), nil
return c.Secret(v.uri), nil
}

// serviceValue is a pflag.Value that builds a dagger.Service from a host:port
Expand Down
3 changes: 2 additions & 1 deletion cmd/dagger/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"dagger.io/dagger/telemetry"
"github.com/dagger/dagger/dagql/idtui"
"github.com/dagger/dagger/engine/client"
"github.com/dagger/dagger/engine/client/pathutil"
"github.com/dagger/dagger/engine/slog"
)

Expand Down Expand Up @@ -680,7 +681,7 @@ func handleResponse(returnType *modTypeDef, response any, o, e io.Writer) error
if err := writeOutputFile(outputPath, buf); err != nil {
return fmt.Errorf("couldn't write output to file: %w", err)
}
path, err := client.Abs(outputPath)
path, err := pathutil.Abs(outputPath)
if err != nil {
// don't fail because at this point the output has been saved successfully
slog.Warn("Failed to get absolute path", "error", err)
Expand Down
6 changes: 3 additions & 3 deletions cmd/dagger/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import (
"github.com/dagger/dagger/dagql/dagui"
"github.com/dagger/dagger/dagql/idtui"
"github.com/dagger/dagger/engine"
"github.com/dagger/dagger/engine/client"
"github.com/dagger/dagger/engine/client/pathutil"
"github.com/dagger/dagger/engine/slog"
enginetel "github.com/dagger/dagger/engine/telemetry"
)
Expand Down Expand Up @@ -388,12 +388,12 @@ func NormalizeWorkdir(workdir string) (string, error) {

if workdir == "" {
var err error
workdir, err = client.Getwd()
workdir, err = pathutil.Getwd()
if err != nil {
return "", err
}
}
workdir, err := client.Abs(workdir)
workdir, err := pathutil.Abs(workdir)
if err != nil {
return "", err
}
Expand Down
15 changes: 8 additions & 7 deletions cmd/dagger/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/dagger/dagger/analytics"
"github.com/dagger/dagger/core/modules"
"github.com/dagger/dagger/engine/client"
"github.com/dagger/dagger/engine/client/pathutil"
"github.com/dagger/dagger/engine/slog"
)

Expand Down Expand Up @@ -139,7 +140,7 @@ If --sdk is specified, the given SDK is installed in the module. You can do this
dag := engineClient.Dagger()

// default the module source root to the current working directory if it doesn't exist yet
cwd, err := client.Getwd()
cwd, err := pathutil.Getwd()
if err != nil {
return fmt.Errorf("failed to get current working directory: %w", err)
}
Expand Down Expand Up @@ -187,7 +188,7 @@ If --sdk is specified, the given SDK is installed in the module. You can do this

if moduleSourcePath != "" {
// ensure source path is relative to the source root
sourceAbsPath, err := client.Abs(moduleSourcePath)
sourceAbsPath, err := pathutil.Abs(moduleSourcePath)
if err != nil {
return fmt.Errorf("failed to get absolute source path for %s: %w", moduleSourcePath, err)
}
Expand Down Expand Up @@ -259,7 +260,7 @@ var moduleInstallCmd = &cobra.Command{
}
if depSrcKind == dagger.ModuleSourceKindLocalSource {
// need to ensure that local dep paths are relative to the parent root source
depAbsPath, err := client.Abs(depRefStr)
depAbsPath, err := pathutil.Abs(depRefStr)
if err != nil {
return fmt.Errorf("failed to get dep absolute path for %s: %w", depRefStr, err)
}
Expand Down Expand Up @@ -492,7 +493,7 @@ This command is idempotent: you can run it at any time, any number of times. It
}
if developSourcePath != "" {
// ensure source path is relative to the source root
sourceAbsPath, err := client.Abs(developSourcePath)
sourceAbsPath, err := pathutil.Abs(developSourcePath)
if err != nil {
return fmt.Errorf("failed to get absolute source path for %s: %w", developSourcePath, err)
}
Expand Down Expand Up @@ -782,12 +783,12 @@ func getModuleConfigurationForSourceRef(
srcRefStr = findupConfigDir
}

conf.LocalRootSourcePath, err = client.Abs(srcRefStr)
conf.LocalRootSourcePath, err = pathutil.Abs(srcRefStr)
if err != nil {
return nil, fmt.Errorf("failed to get absolute path for %s: %w", srcRefStr, err)
}
if filepath.IsAbs(srcRefStr) {
cwd, err := client.Getwd()
cwd, err := pathutil.Getwd()
if err != nil {
return nil, fmt.Errorf("failed to get current working directory: %w", err)
}
Expand Down Expand Up @@ -842,7 +843,7 @@ func findUp(curDirPath string) (string, bool, error) {
}

// didn't exist, try parent unless we've hit the root or a git repo checkout root
curDirAbsPath, err := client.Abs(curDirPath)
curDirAbsPath, err := pathutil.Abs(curDirPath)
if err != nil {
return "", false, fmt.Errorf("failed to get absolute path for %s: %w", curDirPath, err)
}
Expand Down
3 changes: 3 additions & 0 deletions cmd/init/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"golang.org/x/sys/unix"

"github.com/dagger/dagger/engine/client"
"github.com/dagger/dagger/engine/client/secretprovider"
"github.com/dagger/dagger/engine/session"
)

Expand Down Expand Up @@ -183,6 +184,8 @@ func serveSession() {
ctx := context.Background()

attachables := []bksession.Attachable{
// secrets
secretprovider.NewSecretProvider(),
// sockets
client.SocketProvider{EnableHostNetworkAccess: true},
// host=>container networking
Expand Down
2 changes: 1 addition & 1 deletion core/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (host *Host) SetSecretFile(ctx context.Context, srv *dagql.Server, secretNa
}

err = srv.Select(ctx, srv.Root(), &i, dagql.Selector{
Field: "secret",
Field: "loadSecretFromName",
Args: []dagql.NamedInput{
{
Name: "name",
Expand Down
2 changes: 1 addition & 1 deletion core/integration/module_call_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,7 @@ func (m *Test) Insecure(ctx context.Context, token *dagger.Secret) (string, erro

t.Run("invalid source", func(ctx context.Context, t *testctx.T) {
_, err := modGen.With(daggerCall("insecure", "--token", "wtf:HUH")).Stdout(ctx)
requireErrOut(t, err, `unsupported secret arg source: "wtf"`)
requireErrOut(t, err, `unsupported secret provider: "wtf"`)
})
})

Expand Down
2 changes: 1 addition & 1 deletion core/integration/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3115,7 +3115,7 @@ import (
type Leaker struct {}

func (l *Leaker) Leak(ctx context.Context) error {
secret, _ := dag.Secret("mysecret").Plaintext(ctx)
secret, _ := dag.LoadSecretFromName("mysecret").Plaintext(ctx)
fmt.Println("trying to read secret:", secret)
return nil
}
Expand Down
6 changes: 3 additions & 3 deletions core/schema/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -1952,9 +1952,9 @@ func (s *containerSchema) withRegistryAuth(ctx context.Context, parent *core.Con
if err != nil {
return nil, err
}
secretBytes, ok := secretStore.GetSecretPlaintext(secret.Self.IDDigest)
if !ok {
return nil, fmt.Errorf("secret %s not found", secret.Self.IDDigest)
secretBytes, err := secretStore.GetSecretPlaintext(ctx, secret.Self.IDDigest)
if err != nil {
return nil, err
}

auth, err := parent.Query.Auth(ctx)
Expand Down
4 changes: 2 additions & 2 deletions core/schema/modulesource.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"github.com/dagger/dagger/core/modules"
"github.com/dagger/dagger/dagql"
"github.com/dagger/dagger/engine/buildkit"
"github.com/dagger/dagger/engine/client"
"github.com/dagger/dagger/engine/client/pathutil"
"github.com/dagger/dagger/engine/slog"
"github.com/dagger/dagger/engine/vcs"
"github.com/go-git/go-git/v5/plumbing/transport"
Expand Down Expand Up @@ -57,7 +57,7 @@ func (s *moduleSchema) moduleSource(ctx context.Context, query *core.Query, args
return nil, fmt.Errorf("failed to stat caller's current working directory: %w", err)
}

relPath, err := client.LexicalRelativePath(cwdStat.Path, parsed.modPath)
relPath, err := pathutil.LexicalRelativePath(cwdStat.Path, parsed.modPath)
if err != nil {
return nil, err
}
Expand Down
Loading