Authenticate Handler API and SDK, and much more (#183)
This commit is contained in:
1 parent
acce195dcb
commit
28dbcd84bd
89 files changed
+5296
-1943
No files matched your search
@@ -18,6 +18,7 @@ import (
|
||||
"github.com/mitchellh/cli"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/posener/complete"
|
||||
"github.com/zalando/go-keyring"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -26,6 +27,8 @@ const (
|
||||
|
||||
// NotSetValue is a flag value for a not-set value
|
||||
NotSetValue = "(not set)"
|
||||
|
||||
envTokenName = "WATCHTOWER_TOKEN_NAME"
|
||||
)
|
||||
|
||||
// reRemoveWhitespace is a regular expression for stripping whitespace from
|
||||
@@ -53,6 +56,7 @@ type Command struct {
|
||||
|
||||
flagFormat string
|
||||
flagField string
|
||||
FlagTokenName string
|
||||
flagOutputCurlString bool
|
||||
|
||||
client *api.Client
|
||||
@@ -161,6 +165,26 @@ func (c *Command) Client() (*api.Client, error) {
|
||||
c.client.SetMaxRetries(0)
|
||||
}
|
||||
|
||||
if c.client.Token() == "" {
|
||||
tokenName := "default"
|
||||
if c.FlagTokenName != "" {
|
||||
tokenName = c.FlagTokenName
|
||||
}
|
||||
if tokenName != "none" {
|
||||
token, err := keyring.Get("HashiCorp Watchtower Auth Token", tokenName)
|
||||
if err != nil {
|
||||
if err == keyring.ErrNotFound {
|
||||
c.UI.Info("No saved credential found, continuing without")
|
||||
} else {
|
||||
c.UI.Error(fmt.Sprintf("Error reading auth token from system credential store: %s", err))
|
||||
}
|
||||
}
|
||||
if token != "" {
|
||||
c.client.SetToken(token)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return c.client, nil
|
||||
}
|
||||
|
||||
@@ -169,6 +193,7 @@ type FlagSetBit uint
|
||||
const (
|
||||
FlagSetNone FlagSetBit = 1 << iota
|
||||
FlagSetHTTP
|
||||
FlagSetClient
|
||||
FlagSetOutputField
|
||||
FlagSetOutputFormat
|
||||
)
|
||||
@@ -274,6 +299,17 @@ func (c *Command) FlagSet(bit FlagSetBit) *FlagSets {
|
||||
"is highly discouraged as it decreases the security of data " +
|
||||
"transmissions to and from the Watchtower server.",
|
||||
})
|
||||
}
|
||||
|
||||
if bit&FlagSetClient != 0 {
|
||||
f := set.NewFlagSet("Client Options")
|
||||
|
||||
f.StringVar(&StringVar{
|
||||
Name: "token-name",
|
||||
Target: &c.FlagTokenName,
|
||||
EnvVar: envTokenName,
|
||||
Usage: "If specified, the given value will be used as the name when storing the token in the system credential store. This can allow switching user identities for different commands.",
|
||||
})
|
||||
|
||||
f.BoolVar(&BoolVar{
|
||||
Name: "output-curl-string",
|
||||
|
||||
@@ -3,11 +3,15 @@ package base
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"sort"
|
||||
"strings"
|
||||
"unicode"
|
||||
"unicode/utf8"
|
||||
|
||||
"github.com/ghodss/yaml"
|
||||
"github.com/mitchellh/cli"
|
||||
"github.com/mitchellh/go-wordwrap"
|
||||
"github.com/ryanuber/columnize"
|
||||
)
|
||||
|
||||
@@ -17,6 +21,39 @@ const (
|
||||
hopeDelim = "♨"
|
||||
)
|
||||
|
||||
// This is adapted from the code in the strings package for TrimSpace
|
||||
var asciiSpace = [256]uint8{'\t': 1, '\n': 1, '\v': 1, '\f': 1, '\r': 1, ' ': 1}
|
||||
|
||||
func trimSpaceRight(in string) string {
|
||||
for stop := len(in); stop > 0; stop-- {
|
||||
c := in[stop-1]
|
||||
if c >= utf8.RuneSelf {
|
||||
return strings.TrimFunc(in[:stop], unicode.IsSpace)
|
||||
}
|
||||
if asciiSpace[c] == 0 {
|
||||
return in[0:stop]
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func WrapForHelpText(lines []string) string {
|
||||
var ret []string
|
||||
for _, line := range lines {
|
||||
line = trimSpaceRight(line)
|
||||
trimmed := strings.TrimSpace(line)
|
||||
diff := uint(len(line) - len(trimmed))
|
||||
wrapped := wordwrap.WrapString(trimmed, TermWidth-diff)
|
||||
splitWrapped := strings.Split(wrapped, "\n")
|
||||
for i := range splitWrapped {
|
||||
splitWrapped[i] = fmt.Sprintf("%s%s", strings.Repeat(" ", int(diff)), strings.TrimSpace(splitWrapped[i]))
|
||||
}
|
||||
ret = append(ret, strings.Join(splitWrapped, "\n"))
|
||||
}
|
||||
|
||||
return strings.Join(ret, "\n")
|
||||
}
|
||||
|
||||
type FormatOptions struct {
|
||||
Format string
|
||||
}
|
||||
@@ -25,7 +62,7 @@ type FormatOptions struct {
|
||||
type JsonFormatter struct{}
|
||||
|
||||
func (j JsonFormatter) Format(data interface{}) ([]byte, error) {
|
||||
return json.MarshalIndent(data, "", " ")
|
||||
return json.Marshal(data)
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -346,3 +383,29 @@ func looksLikeDuration(k string) bool {
|
||||
k == "duration" || strings.HasSuffix(k, "_duration") ||
|
||||
k == "lease_max" || k == "ttl_max"
|
||||
}
|
||||
|
||||
type Formatter interface {
|
||||
//Output(ui cli.Ui, secret *api.Secret, data interface{}) error
|
||||
Format(data interface{}) ([]byte, error)
|
||||
}
|
||||
|
||||
var Formatters = map[string]Formatter{
|
||||
"json": JsonFormatter{},
|
||||
"table": TableFormatter{},
|
||||
"yaml": YamlFormatter{},
|
||||
"yml": YamlFormatter{},
|
||||
}
|
||||
|
||||
func Format(ui cli.Ui) string {
|
||||
switch ui.(type) {
|
||||
case *WatchtowerUI:
|
||||
return ui.(*WatchtowerUI).Format
|
||||
}
|
||||
|
||||
format := os.Getenv(EnvWatchtowerCLIFormat)
|
||||
if format == "" {
|
||||
format = "table"
|
||||
}
|
||||
|
||||
return format
|
||||
}
|
||||
@@ -27,6 +27,7 @@ import (
|
||||
"github.com/hashicorp/watchtower/globals"
|
||||
"github.com/hashicorp/watchtower/internal/db"
|
||||
"github.com/hashicorp/watchtower/internal/iam"
|
||||
"github.com/hashicorp/watchtower/internal/servers/controller/handlers/authenticate"
|
||||
"github.com/hashicorp/watchtower/version"
|
||||
"github.com/jinzhu/gorm"
|
||||
"github.com/mitchellh/cli"
|
||||
@@ -59,7 +60,8 @@ type Server struct {
|
||||
|
||||
Listeners []*ServerListener
|
||||
|
||||
DefaultOrgId string
|
||||
DefaultOrgId string
|
||||
DevAuthMethodId string
|
||||
|
||||
DevDatabaseUrl string
|
||||
DevDatabaseCleanupFunc func() error
|
||||
@@ -418,40 +420,86 @@ func (b *Server) CreateDevDatabase(dialect string) error {
|
||||
cancel()
|
||||
}()
|
||||
|
||||
var scope *iam.Scope
|
||||
var orgScope *iam.Scope
|
||||
if b.DefaultOrgId != "" {
|
||||
scope, err = repo.LookupScope(ctx, b.DefaultOrgId)
|
||||
orgScope, err = repo.LookupScope(ctx, b.DefaultOrgId)
|
||||
if err != nil {
|
||||
c()
|
||||
return fmt.Errorf("error looking up existing scope with org ID %q: %w", b.DefaultOrgId, err)
|
||||
}
|
||||
if scope != nil {
|
||||
goto INFO
|
||||
}
|
||||
}
|
||||
|
||||
scope, err = iam.NewOrg()
|
||||
if err != nil {
|
||||
c()
|
||||
return fmt.Errorf("error creating new org scope: %w", err)
|
||||
}
|
||||
scope, err = repo.CreateScope(ctx, scope, iam.WithPublicId(b.DefaultOrgId))
|
||||
if err != nil {
|
||||
c()
|
||||
return fmt.Errorf("error persisting new org scope: %w", err)
|
||||
}
|
||||
if b.DefaultOrgId != "" {
|
||||
if scope.GetPublicId() != b.DefaultOrgId {
|
||||
if orgScope == nil {
|
||||
orgScope, err = iam.NewOrg()
|
||||
if err != nil {
|
||||
c()
|
||||
return fmt.Errorf("expected org ID %q, got %q after persisting", b.DefaultOrgId, scope.GetPublicId())
|
||||
return fmt.Errorf("error creating new org scope: %w", err)
|
||||
}
|
||||
orgScope, err = repo.CreateScope(ctx, orgScope, iam.WithPublicId(b.DefaultOrgId))
|
||||
if err != nil {
|
||||
c()
|
||||
return fmt.Errorf("error persisting new org scope: %w", err)
|
||||
}
|
||||
if b.DefaultOrgId != "" {
|
||||
if orgScope.GetPublicId() != b.DefaultOrgId {
|
||||
c()
|
||||
return fmt.Errorf("expected org ID %q, got %q after persisting", b.DefaultOrgId, orgScope.GetPublicId())
|
||||
}
|
||||
} else {
|
||||
b.DefaultOrgId = orgScope.GetPublicId()
|
||||
}
|
||||
} else {
|
||||
b.DefaultOrgId = scope.GetPublicId()
|
||||
}
|
||||
|
||||
INFO:
|
||||
b.InfoKeys = append(b.InfoKeys, "dev org id")
|
||||
ar, err := iam.NewRole(orgScope.PublicId)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error creating in memory role for anon authen: %w", err)
|
||||
}
|
||||
authenRole, err := repo.CreateRole(ctx, ar, iam.WithDescription("role for anon authen"))
|
||||
if err != nil {
|
||||
return fmt.Errorf("error creating role for anon authen: %w", err)
|
||||
}
|
||||
if _, err := repo.AddRoleGrants(ctx, authenRole.PublicId, authenRole.Version, []string{"type=auth-method;actions=list,authenticate"}); err != nil {
|
||||
return fmt.Errorf("error creating grant for anon authen: %w", err)
|
||||
}
|
||||
if _, err := repo.AddPrincipalRoles(ctx, authenRole.PublicId, authenRole.Version+1, []string{"u_anon"}, nil); err != nil {
|
||||
return fmt.Errorf("error adding principal to role for anon authen: %w", err)
|
||||
}
|
||||
|
||||
pr, err := iam.NewRole(orgScope.PublicId)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error creating in memory role for default dev grants: %w", err)
|
||||
}
|
||||
defPermsRole, err := repo.CreateRole(ctx, pr, iam.WithDescription("role for def grants"))
|
||||
if err != nil {
|
||||
return fmt.Errorf("error creating role for default dev grants: %w", err)
|
||||
}
|
||||
if _, err := repo.AddRoleGrants(ctx, defPermsRole.PublicId, defPermsRole.Version, []string{"id=*;actions=*"}); err != nil {
|
||||
return fmt.Errorf("error creating grant for default dev grants: %w", err)
|
||||
}
|
||||
if _, err := repo.AddPrincipalRoles(ctx, defPermsRole.PublicId, defPermsRole.Version+1, []string{"u_auth"}, nil); err != nil {
|
||||
return fmt.Errorf("error adding principal to role for default dev grants: %w", err)
|
||||
}
|
||||
|
||||
// TODO: Remove this when Auth Account repo is in place.
|
||||
authenticate.OrgScope = orgScope.GetPublicId()
|
||||
insert := `insert into auth_method
|
||||
(public_id, scope_id)
|
||||
values
|
||||
($1, $2);`
|
||||
amId := b.DevAuthMethodId
|
||||
if amId == "" {
|
||||
amId = "am_1234567890"
|
||||
}
|
||||
authenticate.RWDb.Store(rw)
|
||||
_, err = b.Database.DB().Exec(insert, amId, orgScope.GetPublicId())
|
||||
if err != nil {
|
||||
c()
|
||||
return err
|
||||
}
|
||||
|
||||
b.InfoKeys = append(b.InfoKeys, "dev org id", "dev auth method id")
|
||||
b.Info["dev org id"] = b.DefaultOrgId
|
||||
b.Info["dev auth method id"] = amId
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
package base
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"github.com/mitchellh/cli"
|
||||
"golang.org/x/crypto/ssh/terminal"
|
||||
)
|
||||
|
||||
type WatchtowerUI struct {
|
||||
cli.Ui
|
||||
Format string
|
||||
}
|
||||
|
||||
var TermWidth uint = 80
|
||||
|
||||
func init() {
|
||||
width, _, err := terminal.GetSize(int(os.Stdin.Fd()))
|
||||
if err == nil {
|
||||
TermWidth = uint(width)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user