G2 token-stand-in-goes (Go half): TokenFile, fromFile, DefaultStateDir, SourceFile, the line at start naming the source and the two env lines go. Builds: design 47 H2 H-A-575, H-A-602, H-A-642
65 lines
2.3 KiB
Go
65 lines
2.3 KiB
Go
package forgehttp
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"strings"
|
|
"testing"
|
|
|
|
"git.novox.be/novox/mesh-sdk/go/seal"
|
|
)
|
|
|
|
// H-A-575: a Go bundle reads its token from the vault alone and says it did at start; whatever the vault refuses, no
|
|
// token file stands in (the stand-in of H-A-564 is gone).
|
|
func TestATokenComesFromTheVaultAloneAndNoFileStandsInWhenTheVaultRefuses(t *testing.T) {
|
|
vaultHolds := func(token string) Ask {
|
|
return func(address string, args any) (json.RawMessage, error) {
|
|
a := args.(map[string]any)
|
|
value, _ := json.Marshal(map[string]any{"token": token})
|
|
box, err := seal.Seal(string(value), a["public_key"].(string))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return json.Marshal(map[string]any{"generation": 3, "sealed": box})
|
|
}
|
|
}
|
|
vaultSays := func(said string) Ask {
|
|
return func(string, any) (json.RawMessage, error) { return nil, errors.New(said) }
|
|
}
|
|
for _, c := range []struct {
|
|
name, account string
|
|
ask Ask
|
|
token, err string
|
|
}{
|
|
{"the vault hands it", "mesh-tools", vaultHolds("vault-tools"), "vault-tools", ""},
|
|
{"the vault refuses the tool runner: no file is read", "mesh-packages",
|
|
vaultSays("novox.node-tools is no module's account (<machine>.<module>), so it has no own secret here"), "", "no module's account"},
|
|
{"another module's account: no file is read", "mesh-tools",
|
|
vaultSays("module/gitea/mesh-tools is gitea's own secret, handed only to an account of gitea"), "", "handed only to an account of gitea"},
|
|
{"the vault holds none", "mesh-tools", vaultSays("the vault holds no secret named module/gitea/mesh-tools"), "", "holds no secret"},
|
|
} {
|
|
t.Run(c.name, func(t *testing.T) {
|
|
v, err := NewVaultToken(c.account, c.ask)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var said []string
|
|
Start(func(f string, a ...any) { said = append(said, fmt.Sprintf(f, a...)) }, v)
|
|
got, err := v.Token()
|
|
if c.err != "" {
|
|
if err == nil || !strings.Contains(err.Error(), c.err) {
|
|
t.Fatalf("token %q, error %v; want an error saying %q", got, err, c.err)
|
|
}
|
|
return
|
|
}
|
|
if err != nil || got != c.token {
|
|
t.Fatalf("token %q (%v); want %q", got, err, c.token)
|
|
}
|
|
if len(said) != 1 || strings.Contains(said[0], c.token) || strings.Contains(said[0], "file") {
|
|
t.Fatalf("said %q: one line, never the token and naming no file", said)
|
|
}
|
|
})
|
|
}
|
|
}
|