feat: port influx auth
command (#152)
This commit is contained in:
@ -71,7 +71,7 @@ func (c Client) Create(ctx context.Context, params *CreateParams) error {
|
||||
}
|
||||
|
||||
// verify an existing token with the same username doesn't already exist
|
||||
auths, err := c.LegacyAuthorizationsApi.GetAuthorizations(ctx).Token(params.Username).Execute()
|
||||
auths, err := c.LegacyAuthorizationsApi.GetLegacyAuthorizations(ctx).Token(params.Username).Execute()
|
||||
if apiError, ok := err.(api.ApiError); ok {
|
||||
if apiError.ErrorCode() != api.ERRORCODE_NOT_FOUND {
|
||||
return fmt.Errorf("failed to verify username %q has no auth: %w", params.Username, err)
|
||||
@ -123,17 +123,17 @@ func (c Client) Create(ctx context.Context, params *CreateParams) error {
|
||||
Token: ¶ms.Username,
|
||||
}
|
||||
|
||||
newAuth, err := c.LegacyAuthorizationsApi.PostAuthorizations(ctx).LegacyAuthorizationPostRequest(authReq).Execute()
|
||||
newAuth, err := c.LegacyAuthorizationsApi.PostLegacyAuthorizations(ctx).LegacyAuthorizationPostRequest(authReq).Execute()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create new authorization: %w", err)
|
||||
}
|
||||
|
||||
if password != "" {
|
||||
err := c.LegacyAuthorizationsApi.PostAuthorizationsIDPassword(ctx, newAuth.GetId()).
|
||||
err := c.LegacyAuthorizationsApi.PostLegacyAuthorizationsIDPassword(ctx, newAuth.GetId()).
|
||||
PasswordResetBody(api.PasswordResetBody{Password: password}).
|
||||
Execute()
|
||||
if err != nil {
|
||||
_ = c.LegacyAuthorizationsApi.DeleteAuthorizationsID(ctx, newAuth.GetId()).Execute()
|
||||
_ = c.LegacyAuthorizationsApi.DeleteLegacyAuthorizationsID(ctx, newAuth.GetId()).Execute()
|
||||
return fmt.Errorf("failed to set password for %q: %w", params.Username, err)
|
||||
}
|
||||
}
|
||||
@ -145,7 +145,7 @@ func (c Client) Create(ctx context.Context, params *CreateParams) error {
|
||||
|
||||
ps := make([]string, 0, len(newAuth.Permissions))
|
||||
for _, p := range newAuth.Permissions {
|
||||
ps = append(ps, permString(p))
|
||||
ps = append(ps, p.String())
|
||||
}
|
||||
|
||||
return c.printV1Tokens(&v1PrintOpts{
|
||||
@ -171,11 +171,11 @@ func (c Client) Remove(ctx context.Context, params *RemoveParams) error {
|
||||
return err
|
||||
}
|
||||
|
||||
auth, err := c.LegacyAuthorizationsApi.GetAuthorizationsID(ctx, id).Execute()
|
||||
auth, err := c.LegacyAuthorizationsApi.GetLegacyAuthorizationsID(ctx, id).Execute()
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not find Authorization from ID %q: %w", id, err)
|
||||
}
|
||||
err = c.LegacyAuthorizationsApi.DeleteAuthorizationsID(ctx, id).Execute()
|
||||
err = c.LegacyAuthorizationsApi.DeleteLegacyAuthorizationsID(ctx, id).Execute()
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not remove Authorization with ID %q: %w", id, err)
|
||||
}
|
||||
@ -187,7 +187,7 @@ func (c Client) Remove(ctx context.Context, params *RemoveParams) error {
|
||||
|
||||
ps := make([]string, 0, len(auth.GetPermissions()))
|
||||
for _, p := range auth.GetPermissions() {
|
||||
ps = append(ps, permString(p))
|
||||
ps = append(ps, p.String())
|
||||
}
|
||||
|
||||
return c.printV1Tokens(&v1PrintOpts{
|
||||
@ -212,7 +212,7 @@ type ListParams struct {
|
||||
}
|
||||
|
||||
func (c Client) List(ctx context.Context, params *ListParams) error {
|
||||
req := c.LegacyAuthorizationsApi.GetAuthorizations(ctx)
|
||||
req := c.LegacyAuthorizationsApi.GetLegacyAuthorizations(ctx)
|
||||
|
||||
if params.User != "" {
|
||||
req = req.User(params.User)
|
||||
@ -242,7 +242,7 @@ func (c Client) List(ctx context.Context, params *ListParams) error {
|
||||
for _, a := range auths.GetAuthorizations() {
|
||||
var permissions []string
|
||||
for _, p := range a.GetPermissions() {
|
||||
permissions = append(permissions, permString(p))
|
||||
permissions = append(permissions, p.String())
|
||||
}
|
||||
|
||||
usr, err := c.UsersApi.GetUsersID(ctx, a.GetUserID()).Execute()
|
||||
@ -274,7 +274,7 @@ func (c Client) SetActive(ctx context.Context, params *ActiveParams, active bool
|
||||
return err
|
||||
}
|
||||
|
||||
req := c.LegacyAuthorizationsApi.PatchAuthorizationsID(ctx, id)
|
||||
req := c.LegacyAuthorizationsApi.PatchLegacyAuthorizationsID(ctx, id)
|
||||
var status string
|
||||
if active {
|
||||
status = "active"
|
||||
@ -297,7 +297,7 @@ func (c Client) SetActive(ctx context.Context, params *ActiveParams, active bool
|
||||
|
||||
ps := make([]string, 0, len(auth.GetPermissions()))
|
||||
for _, p := range auth.GetPermissions() {
|
||||
ps = append(ps, permString(p))
|
||||
ps = append(ps, p.String())
|
||||
}
|
||||
|
||||
return c.printV1Tokens(&v1PrintOpts{
|
||||
@ -333,7 +333,7 @@ func (c Client) SetPassword(ctx context.Context, params *SetPasswordParams) erro
|
||||
password = pass
|
||||
}
|
||||
|
||||
err = c.LegacyAuthorizationsApi.PostAuthorizationsIDPassword(ctx, id).
|
||||
err = c.LegacyAuthorizationsApi.PostLegacyAuthorizationsIDPassword(ctx, id).
|
||||
PasswordResetBody(api.PasswordResetBody{Password: password}).
|
||||
Execute()
|
||||
if err != nil {
|
||||
@ -387,26 +387,12 @@ func (c Client) printV1Tokens(params *v1PrintOpts) error {
|
||||
return c.PrintTable(headers, rows...)
|
||||
}
|
||||
|
||||
func permString(p api.Permission) string {
|
||||
ret := p.GetAction() + ":"
|
||||
r := p.GetResource()
|
||||
|
||||
if r.GetOrgID() != "" {
|
||||
ret += "orgs/" + r.GetOrgID()
|
||||
}
|
||||
ret += "/" + r.GetType()
|
||||
if r.GetId() != "" {
|
||||
ret += "/" + r.GetId()
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
func (c Client) getAuthReqID(ctx context.Context, params *AuthLookupParams) (id string, err error) {
|
||||
if params.ID.Valid() {
|
||||
id = params.ID.String()
|
||||
} else {
|
||||
var auths api.Authorizations
|
||||
auths, err = c.LegacyAuthorizationsApi.GetAuthorizations(ctx).Token(params.Username).Execute()
|
||||
auths, err = c.LegacyAuthorizationsApi.GetLegacyAuthorizations(ctx).Token(params.Username).Execute()
|
||||
if err != nil || len(auths.GetAuthorizations()) == 0 {
|
||||
err = fmt.Errorf("could not find v1 auth with token (username) %q: %w", params.Username, err)
|
||||
} else {
|
||||
|
Reference in New Issue
Block a user