๐ Hey!
I've been getting weird errors when creating secrets inside a module's constructor and trying to use them later on.
The actual use case is exchanging OIDC tokens for a token authenticating to a service, which I then need to use later on.
Here is a reproduction example:
I have a module that generates and stores a secret on initialization:
package main
import (
"context"
"dagger/generator/internal/dagger"
)
type Generator struct {
// +private
Password *dagger.Secret
}
func New() *Generator {
return &Generator{
Password: dag.SetSecret("pass", "admin"),
}
}
func (m *Generator) Gen(ctx context.Context, name string) error {
_, err := m.Password.Plaintext(ctx)
return err
}
I have a second module that depends on the first one and wraps calls to it like so:
package main
import (
"context"
)
type Keychain struct{}
func (m *Keychain) Get(ctx context.Context, name string) error {
return dag.Generator().Gen(ctx, name)
}
Finally, I use the second module from a contextual module:
package main
import (
"context"
"fmt"
)
type Mymodule struct{}
func (m *Mymodule) Issue(ctx context.Context) error {
kc := dag.Keychain()
err := kc.Get(ctx, "a")
if err != nil {
return fmt.Errorf("first get: %w", err)
}
err = kc.Get(ctx, "b")
if err != nil {
return fmt.Errorf("second get: %w", err)
}
return nil
}
The first call to kc.Get is succesful.
However, the second one fails (see error in comment).
Any insights on what is going on?
How could I create that secret in a way that I know will not fail?
Thank you for your help!