runtime: context: support secret environment variables

Signed-off-by: Andrea Luzzardi <aluzzardi@gmail.com>
This commit is contained in:
Andrea Luzzardi 2021-11-24 16:24:29 -08:00
parent 131551f9cd
commit db55cf7583

48
plan/task/secretenv.go Normal file
View File

@ -0,0 +1,48 @@
package task
import (
"context"
"fmt"
"os"
"cuelang.org/go/cue"
"github.com/rs/zerolog/log"
"go.dagger.io/dagger/compiler"
"go.dagger.io/dagger/plancontext"
"go.dagger.io/dagger/solver"
)
func init() {
Register("SecretEnv", func() Task { return &secretEnvTask{} })
}
type secretEnvTask struct {
}
func (c secretEnvTask) Run(ctx context.Context, pctx *plancontext.Context, _ solver.Solver, v *compiler.Value) (*compiler.Value, error) {
lg := log.Ctx(ctx)
var secretEnv struct {
Envvar string
}
if err := v.Decode(&secretEnv); err != nil {
return nil, err
}
lg.Debug().Str("envvar", secretEnv.Envvar).Msg("loading secret")
env := os.Getenv(secretEnv.Envvar)
if env == "" {
return nil, fmt.Errorf("environment variable %q not set", secretEnv.Envvar)
}
id := pctx.Secrets.Register(&plancontext.Secret{
PlainText: env,
})
out := compiler.NewValue()
if err := out.FillPath(cue.ParsePath("contents.id"), id); err != nil {
return nil, err
}
return out, nil
}