2017-09-28 20:44:30 +00:00
|
|
|
package nomad
|
|
|
|
|
|
|
|
import (
|
2018-01-08 18:31:38 +00:00
|
|
|
"context"
|
2017-09-28 20:44:30 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
nomadapi "github.com/hashicorp/nomad/api"
|
2018-08-16 19:48:23 +00:00
|
|
|
"github.com/hashicorp/vault/helper/testhelpers"
|
2019-04-22 16:26:10 +00:00
|
|
|
"github.com/hashicorp/vault/helper/testhelpers/docker"
|
2019-04-12 21:54:35 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/logical"
|
2017-09-28 20:44:30 +00:00
|
|
|
"github.com/mitchellh/mapstructure"
|
|
|
|
)
|
|
|
|
|
2020-09-15 14:01:26 +00:00
|
|
|
type Config struct {
|
|
|
|
docker.ServiceURL
|
|
|
|
Token string
|
|
|
|
}
|
2017-09-28 20:44:30 +00:00
|
|
|
|
2020-09-15 14:01:26 +00:00
|
|
|
func (c *Config) APIConfig() *nomadapi.Config {
|
|
|
|
apiConfig := nomadapi.DefaultConfig()
|
|
|
|
apiConfig.Address = c.URL().String()
|
|
|
|
apiConfig.SecretID = c.Token
|
|
|
|
return apiConfig
|
|
|
|
}
|
2017-09-28 20:44:30 +00:00
|
|
|
|
2020-09-15 14:01:26 +00:00
|
|
|
func prepareTestContainer(t *testing.T) (func(), *Config) {
|
|
|
|
if retAddress := os.Getenv("NOMAD_ADDR"); retAddress != "" {
|
|
|
|
s, err := docker.NewServiceURLParse(retAddress)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
return func() {}, &Config{*s, os.Getenv("NOMAD_TOKEN")}
|
2017-09-28 20:44:30 +00:00
|
|
|
}
|
|
|
|
|
2020-09-15 14:01:26 +00:00
|
|
|
runner, err := docker.NewServiceRunner(docker.RunOptions{
|
|
|
|
ImageRepo: "catsby/nomad",
|
|
|
|
ImageTag: "0.8.4",
|
|
|
|
ContainerName: "nomad",
|
|
|
|
Ports: []string{"4646/tcp"},
|
|
|
|
Cmd: []string{"agent", "-dev"},
|
|
|
|
Env: []string{`NOMAD_LOCAL_CONFIG=bind_addr = "0.0.0.0" acl { enabled = true }`},
|
|
|
|
})
|
2017-09-28 20:44:30 +00:00
|
|
|
if err != nil {
|
2020-09-15 14:01:26 +00:00
|
|
|
t.Fatalf("Could not start docker Nomad: %s", err)
|
2017-09-28 20:44:30 +00:00
|
|
|
}
|
|
|
|
|
2020-09-15 14:01:26 +00:00
|
|
|
var nomadToken string
|
|
|
|
svc, err := runner.StartService(context.Background(), func(ctx context.Context, host string, port int) (docker.ServiceConfig, error) {
|
2017-09-28 20:44:30 +00:00
|
|
|
var err error
|
|
|
|
nomadapiConfig := nomadapi.DefaultConfig()
|
2020-09-15 14:01:26 +00:00
|
|
|
nomadapiConfig.Address = fmt.Sprintf("http://%s:%d/", host, port)
|
2017-09-28 20:44:30 +00:00
|
|
|
nomad, err := nomadapi.NewClient(nomadapiConfig)
|
|
|
|
if err != nil {
|
2020-09-15 14:01:26 +00:00
|
|
|
return nil, err
|
2017-09-28 20:44:30 +00:00
|
|
|
}
|
|
|
|
aclbootstrap, _, err := nomad.ACLTokens().Bootstrap(nil)
|
2017-09-28 22:58:41 +00:00
|
|
|
if err != nil {
|
2020-09-15 14:01:26 +00:00
|
|
|
return nil, err
|
2017-09-28 22:58:41 +00:00
|
|
|
}
|
2017-09-28 20:44:30 +00:00
|
|
|
nomadToken = aclbootstrap.SecretID
|
2017-12-16 00:50:20 +00:00
|
|
|
t.Logf("[WARN] Generated Master token: %s", nomadToken)
|
2017-09-28 20:44:30 +00:00
|
|
|
policy := &nomadapi.ACLPolicy{
|
|
|
|
Name: "test",
|
|
|
|
Description: "test",
|
|
|
|
Rules: `namespace "default" {
|
|
|
|
policy = "read"
|
|
|
|
}
|
|
|
|
`,
|
2017-09-29 08:33:58 +00:00
|
|
|
}
|
|
|
|
anonPolicy := &nomadapi.ACLPolicy{
|
|
|
|
Name: "anonymous",
|
|
|
|
Description: "Deny all access for anonymous requests",
|
|
|
|
Rules: `namespace "default" {
|
|
|
|
policy = "deny"
|
|
|
|
}
|
|
|
|
agent {
|
|
|
|
policy = "deny"
|
|
|
|
}
|
|
|
|
node {
|
|
|
|
policy = "deny"
|
|
|
|
}
|
|
|
|
`,
|
2017-09-28 20:44:30 +00:00
|
|
|
}
|
2017-09-28 22:58:41 +00:00
|
|
|
nomadAuthConfig := nomadapi.DefaultConfig()
|
2020-09-15 14:01:26 +00:00
|
|
|
nomadAuthConfig.Address = nomad.Address()
|
2017-09-28 22:58:41 +00:00
|
|
|
nomadAuthConfig.SecretID = nomadToken
|
|
|
|
nomadAuth, err := nomadapi.NewClient(nomadAuthConfig)
|
2021-07-13 11:28:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-09-28 22:58:41 +00:00
|
|
|
_, err = nomadAuth.ACLPolicies().Upsert(policy, nil)
|
2017-09-28 20:44:30 +00:00
|
|
|
if err != nil {
|
2020-09-15 14:01:26 +00:00
|
|
|
return nil, err
|
2017-09-28 20:44:30 +00:00
|
|
|
}
|
2017-09-29 08:33:58 +00:00
|
|
|
_, err = nomadAuth.ACLPolicies().Upsert(anonPolicy, nil)
|
|
|
|
if err != nil {
|
2020-09-15 14:01:26 +00:00
|
|
|
return nil, err
|
2017-09-29 08:33:58 +00:00
|
|
|
}
|
2020-09-15 14:01:26 +00:00
|
|
|
u, _ := docker.NewServiceURLParse(nomadapiConfig.Address)
|
|
|
|
return &Config{
|
|
|
|
ServiceURL: *u,
|
|
|
|
Token: nomadToken,
|
|
|
|
}, nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Could not start docker Nomad: %s", err)
|
2017-09-28 20:44:30 +00:00
|
|
|
}
|
2020-09-15 14:01:26 +00:00
|
|
|
|
|
|
|
return svc.Cleanup, svc.Config.(*Config)
|
2017-09-28 20:44:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestBackend_config_access(t *testing.T) {
|
|
|
|
config := logical.TestBackendConfig()
|
|
|
|
config.StorageView = &logical.InmemStorage{}
|
2018-01-19 06:44:44 +00:00
|
|
|
b, err := Factory(context.Background(), config)
|
2017-09-28 20:44:30 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-09-15 14:01:26 +00:00
|
|
|
cleanup, svccfg := prepareTestContainer(t)
|
2017-09-28 20:44:30 +00:00
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
connData := map[string]interface{}{
|
2020-09-15 14:01:26 +00:00
|
|
|
"address": svccfg.URL().String(),
|
|
|
|
"token": svccfg.Token,
|
2017-09-28 20:44:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
confReq := &logical.Request{
|
|
|
|
Operation: logical.UpdateOperation,
|
|
|
|
Path: "config/access",
|
|
|
|
Storage: config.StorageView,
|
|
|
|
Data: connData,
|
|
|
|
}
|
|
|
|
|
2018-01-08 18:31:38 +00:00
|
|
|
resp, err := b.HandleRequest(context.Background(), confReq)
|
2017-09-28 20:44:30 +00:00
|
|
|
if err != nil || (resp != nil && resp.IsError()) || resp != nil {
|
|
|
|
t.Fatalf("failed to write configuration: resp:%#v err:%s", resp, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
confReq.Operation = logical.ReadOperation
|
2018-01-08 18:31:38 +00:00
|
|
|
resp, err = b.HandleRequest(context.Background(), confReq)
|
2017-09-28 20:44:30 +00:00
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("failed to write configuration: resp:%#v err:%s", resp, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := map[string]interface{}{
|
2018-08-16 19:48:23 +00:00
|
|
|
"address": connData["address"].(string),
|
|
|
|
"max_token_name_length": 0,
|
2017-09-28 20:44:30 +00:00
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(expected, resp.Data) {
|
|
|
|
t.Fatalf("bad: expected:%#v\nactual:%#v\n", expected, resp.Data)
|
|
|
|
}
|
|
|
|
if resp.Data["token"] != nil {
|
|
|
|
t.Fatalf("token should not be set in the response")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBackend_renew_revoke(t *testing.T) {
|
|
|
|
config := logical.TestBackendConfig()
|
|
|
|
config.StorageView = &logical.InmemStorage{}
|
2018-01-19 06:44:44 +00:00
|
|
|
b, err := Factory(context.Background(), config)
|
2017-09-28 20:44:30 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-09-15 14:01:26 +00:00
|
|
|
cleanup, svccfg := prepareTestContainer(t)
|
2017-09-29 08:35:17 +00:00
|
|
|
defer cleanup()
|
2020-09-15 14:01:26 +00:00
|
|
|
|
2017-09-28 20:44:30 +00:00
|
|
|
connData := map[string]interface{}{
|
2020-09-15 14:01:26 +00:00
|
|
|
"address": svccfg.URL().String(),
|
|
|
|
"token": svccfg.Token,
|
2017-09-28 20:44:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
req := &logical.Request{
|
|
|
|
Storage: config.StorageView,
|
|
|
|
Operation: logical.UpdateOperation,
|
|
|
|
Path: "config/access",
|
|
|
|
Data: connData,
|
|
|
|
}
|
2018-01-08 18:31:38 +00:00
|
|
|
resp, err := b.HandleRequest(context.Background(), req)
|
2017-09-28 20:44:30 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-10-31 20:56:56 +00:00
|
|
|
req.Path = "role/test"
|
2017-09-28 20:44:30 +00:00
|
|
|
req.Data = map[string]interface{}{
|
2017-11-29 16:31:17 +00:00
|
|
|
"policies": []string{"policy"},
|
|
|
|
"lease": "6h",
|
2017-09-28 20:44:30 +00:00
|
|
|
}
|
2018-01-08 18:31:38 +00:00
|
|
|
resp, err = b.HandleRequest(context.Background(), req)
|
2017-09-28 20:44:30 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
req.Operation = logical.ReadOperation
|
|
|
|
req.Path = "creds/test"
|
2018-01-08 18:31:38 +00:00
|
|
|
resp, err = b.HandleRequest(context.Background(), req)
|
2017-09-28 20:44:30 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if resp == nil {
|
|
|
|
t.Fatal("resp nil")
|
|
|
|
}
|
|
|
|
if resp.IsError() {
|
|
|
|
t.Fatalf("resp is error: %v", resp.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
generatedSecret := resp.Secret
|
|
|
|
generatedSecret.TTL = 6 * time.Hour
|
|
|
|
|
|
|
|
var d struct {
|
2017-09-29 08:33:58 +00:00
|
|
|
Token string `mapstructure:"secret_id"`
|
|
|
|
Accessor string `mapstructure:"accessor_id"`
|
2017-09-28 20:44:30 +00:00
|
|
|
}
|
|
|
|
if err := mapstructure.Decode(resp.Data, &d); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-03-20 18:54:10 +00:00
|
|
|
t.Logf("[WARN] Generated token: %s with accessor %s", d.Token, d.Accessor)
|
2017-09-28 20:44:30 +00:00
|
|
|
|
|
|
|
// Build a client and verify that the credentials work
|
|
|
|
nomadapiConfig := nomadapi.DefaultConfig()
|
|
|
|
nomadapiConfig.Address = connData["address"].(string)
|
|
|
|
nomadapiConfig.SecretID = d.Token
|
|
|
|
client, err := nomadapi.NewClient(nomadapiConfig)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-10-31 19:11:24 +00:00
|
|
|
t.Log("[WARN] Verifying that the generated token works...")
|
2017-09-29 08:33:58 +00:00
|
|
|
_, err = client.Agent().Members, nil
|
2017-09-28 20:44:30 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
req.Operation = logical.RenewOperation
|
|
|
|
req.Secret = generatedSecret
|
2018-01-08 18:31:38 +00:00
|
|
|
resp, err = b.HandleRequest(context.Background(), req)
|
2017-09-28 20:44:30 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if resp == nil {
|
|
|
|
t.Fatal("got nil response from renew")
|
|
|
|
}
|
|
|
|
|
|
|
|
req.Operation = logical.RevokeOperation
|
2018-01-08 18:31:38 +00:00
|
|
|
resp, err = b.HandleRequest(context.Background(), req)
|
2017-09-28 20:44:30 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2017-09-29 08:33:58 +00:00
|
|
|
// Build a management client and verify that the token does not exist anymore
|
|
|
|
nomadmgmtConfig := nomadapi.DefaultConfig()
|
|
|
|
nomadmgmtConfig.Address = connData["address"].(string)
|
|
|
|
nomadmgmtConfig.SecretID = connData["token"].(string)
|
|
|
|
mgmtclient, err := nomadapi.NewClient(nomadmgmtConfig)
|
2021-07-13 11:28:46 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-09-29 08:33:58 +00:00
|
|
|
|
|
|
|
q := &nomadapi.QueryOptions{
|
|
|
|
Namespace: "default",
|
|
|
|
}
|
|
|
|
|
2017-10-31 19:11:24 +00:00
|
|
|
t.Log("[WARN] Verifying that the generated token does not exist...")
|
2017-09-29 08:33:58 +00:00
|
|
|
_, _, err = mgmtclient.ACLTokens().Info(d.Accessor, q)
|
2017-09-28 20:44:30 +00:00
|
|
|
if err == nil {
|
2017-09-29 08:33:58 +00:00
|
|
|
t.Fatal("err: expected error")
|
2017-09-28 20:44:30 +00:00
|
|
|
}
|
|
|
|
}
|
2017-12-17 15:51:39 +00:00
|
|
|
|
|
|
|
func TestBackend_CredsCreateEnvVar(t *testing.T) {
|
|
|
|
config := logical.TestBackendConfig()
|
|
|
|
config.StorageView = &logical.InmemStorage{}
|
2018-01-19 06:44:44 +00:00
|
|
|
b, err := Factory(context.Background(), config)
|
2017-12-17 15:51:39 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-09-15 14:01:26 +00:00
|
|
|
cleanup, svccfg := prepareTestContainer(t)
|
2017-12-17 15:51:39 +00:00
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
req := logical.TestRequest(t, logical.UpdateOperation, "role/test")
|
|
|
|
req.Data = map[string]interface{}{
|
|
|
|
"policies": []string{"policy"},
|
|
|
|
"lease": "6h",
|
|
|
|
}
|
2018-01-08 18:31:38 +00:00
|
|
|
resp, err := b.HandleRequest(context.Background(), req)
|
2017-12-17 15:51:39 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-09-15 14:01:26 +00:00
|
|
|
os.Setenv("NOMAD_TOKEN", svccfg.Token)
|
2017-12-17 15:51:39 +00:00
|
|
|
defer os.Unsetenv("NOMAD_TOKEN")
|
2020-09-15 14:01:26 +00:00
|
|
|
os.Setenv("NOMAD_ADDR", svccfg.URL().String())
|
2017-12-17 15:51:39 +00:00
|
|
|
defer os.Unsetenv("NOMAD_ADDR")
|
|
|
|
|
|
|
|
req.Operation = logical.ReadOperation
|
|
|
|
req.Path = "creds/test"
|
2018-01-08 18:31:38 +00:00
|
|
|
resp, err = b.HandleRequest(context.Background(), req)
|
2017-12-17 15:51:39 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if resp == nil {
|
|
|
|
t.Fatal("resp nil")
|
|
|
|
}
|
|
|
|
if resp.IsError() {
|
|
|
|
t.Fatalf("resp is error: %v", resp.Error())
|
|
|
|
}
|
|
|
|
}
|
2018-08-16 19:48:23 +00:00
|
|
|
|
|
|
|
func TestBackend_max_token_name_length(t *testing.T) {
|
|
|
|
config := logical.TestBackendConfig()
|
|
|
|
config.StorageView = &logical.InmemStorage{}
|
|
|
|
b, err := Factory(context.Background(), config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-09-15 14:01:26 +00:00
|
|
|
cleanup, svccfg := prepareTestContainer(t)
|
2018-08-16 19:48:23 +00:00
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
title string
|
|
|
|
roleName string
|
|
|
|
tokenLength int
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
title: "Default",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: "ConfigOverride",
|
|
|
|
tokenLength: 64,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: "ConfigOverride-LongName",
|
|
|
|
roleName: "testlongerrolenametoexceed64charsdddddddddddddddddddddddd",
|
|
|
|
tokenLength: 64,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: "Notrim",
|
|
|
|
roleName: "testlongersubrolenametoexceed64charsdddddddddddddddddddddddd",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.title, func(t *testing.T) {
|
|
|
|
// setup config/access
|
|
|
|
connData := map[string]interface{}{
|
2020-09-15 14:01:26 +00:00
|
|
|
"address": svccfg.URL().String(),
|
|
|
|
"token": svccfg.Token,
|
2018-08-16 19:48:23 +00:00
|
|
|
"max_token_name_length": tc.tokenLength,
|
|
|
|
}
|
|
|
|
expected := map[string]interface{}{
|
2020-09-15 14:01:26 +00:00
|
|
|
"address": svccfg.URL().String(),
|
2018-08-16 19:48:23 +00:00
|
|
|
"max_token_name_length": tc.tokenLength,
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedMaxTokenNameLength := maxTokenNameLength
|
|
|
|
if tc.tokenLength != 0 {
|
|
|
|
expectedMaxTokenNameLength = tc.tokenLength
|
|
|
|
}
|
|
|
|
|
|
|
|
confReq := logical.Request{
|
|
|
|
Operation: logical.UpdateOperation,
|
|
|
|
Path: "config/access",
|
|
|
|
Storage: config.StorageView,
|
|
|
|
Data: connData,
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := b.HandleRequest(context.Background(), &confReq)
|
|
|
|
if err != nil || (resp != nil && resp.IsError()) || resp != nil {
|
|
|
|
t.Fatalf("failed to write configuration: resp:%#v err:%s", resp, err)
|
|
|
|
}
|
|
|
|
confReq.Operation = logical.ReadOperation
|
|
|
|
resp, err = b.HandleRequest(context.Background(), &confReq)
|
|
|
|
if err != nil || (resp != nil && resp.IsError()) {
|
|
|
|
t.Fatalf("failed to write configuration: resp:%#v err:%s", resp, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// verify token length is returned in the config/access query
|
|
|
|
if !reflect.DeepEqual(expected, resp.Data) {
|
|
|
|
t.Fatalf("bad: expected:%#v\nactual:%#v\n", expected, resp.Data)
|
|
|
|
}
|
|
|
|
// verify token is not returned
|
|
|
|
if resp.Data["token"] != nil {
|
|
|
|
t.Fatalf("token should not be set in the response")
|
|
|
|
}
|
|
|
|
|
|
|
|
// create a role to create nomad credentials with
|
|
|
|
// Seeds random with current timestamp
|
|
|
|
|
|
|
|
if tc.roleName == "" {
|
|
|
|
tc.roleName = "test"
|
|
|
|
}
|
|
|
|
roleTokenName := testhelpers.RandomWithPrefix(tc.roleName)
|
|
|
|
|
|
|
|
confReq.Path = "role/" + roleTokenName
|
|
|
|
confReq.Operation = logical.UpdateOperation
|
|
|
|
confReq.Data = map[string]interface{}{
|
|
|
|
"policies": []string{"policy"},
|
|
|
|
"lease": "6h",
|
|
|
|
}
|
|
|
|
resp, err = b.HandleRequest(context.Background(), &confReq)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
confReq.Operation = logical.ReadOperation
|
|
|
|
confReq.Path = "creds/" + roleTokenName
|
|
|
|
resp, err = b.HandleRequest(context.Background(), &confReq)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if resp == nil {
|
|
|
|
t.Fatal("resp nil")
|
|
|
|
}
|
|
|
|
if resp.IsError() {
|
|
|
|
t.Fatalf("resp is error: %v", resp.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
// extract the secret, so we can query nomad directly
|
|
|
|
generatedSecret := resp.Secret
|
|
|
|
generatedSecret.TTL = 6 * time.Hour
|
|
|
|
|
|
|
|
var d struct {
|
|
|
|
Token string `mapstructure:"secret_id"`
|
|
|
|
Accessor string `mapstructure:"accessor_id"`
|
|
|
|
}
|
|
|
|
if err := mapstructure.Decode(resp.Data, &d); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build a client and verify that the credentials work
|
|
|
|
nomadapiConfig := nomadapi.DefaultConfig()
|
|
|
|
nomadapiConfig.Address = connData["address"].(string)
|
|
|
|
nomadapiConfig.SecretID = d.Token
|
|
|
|
client, err := nomadapi.NewClient(nomadapiConfig)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// default query options for Nomad queries ... not sure if needed
|
|
|
|
qOpts := &nomadapi.QueryOptions{
|
|
|
|
Namespace: "default",
|
|
|
|
}
|
|
|
|
|
|
|
|
// connect to Nomad and verify the token name does not exceed the
|
|
|
|
// max_token_name_length
|
|
|
|
token, _, err := client.ACLTokens().Self(qOpts)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(token.Name) > expectedMaxTokenNameLength {
|
|
|
|
t.Fatalf("token name exceeds max length (%d): %s (%d)", expectedMaxTokenNameLength, token.Name, len(token.Name))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|