2023-03-15 16:00:52 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2015-04-05 00:53:59 +00:00
|
|
|
package api
|
|
|
|
|
2021-12-21 17:46:56 +00:00
|
|
|
import (
|
|
|
|
"context"
|
2022-03-24 17:58:03 +00:00
|
|
|
"net/http"
|
2021-12-21 17:46:56 +00:00
|
|
|
)
|
2018-07-24 22:49:55 +00:00
|
|
|
|
2016-04-05 15:00:12 +00:00
|
|
|
// TokenAuth is used to perform token backend operations on Vault
|
2015-04-05 00:53:59 +00:00
|
|
|
type TokenAuth struct {
|
|
|
|
c *Client
|
|
|
|
}
|
|
|
|
|
2016-04-05 15:00:12 +00:00
|
|
|
// Token is used to return the client for token-backend API calls
|
2015-04-05 00:54:16 +00:00
|
|
|
func (a *Auth) Token() *TokenAuth {
|
2015-04-05 00:53:59 +00:00
|
|
|
return &TokenAuth{c: a.c}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *TokenAuth) Create(opts *TokenCreateRequest) (*Secret, error) {
|
2022-03-23 21:47:43 +00:00
|
|
|
return c.CreateWithContext(context.Background(), opts)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *TokenAuth) CreateWithContext(ctx context.Context, opts *TokenCreateRequest) (*Secret, error) {
|
|
|
|
ctx, cancelFunc := c.c.withConfiguredTimeout(ctx)
|
|
|
|
defer cancelFunc()
|
|
|
|
|
2022-03-24 17:58:03 +00:00
|
|
|
r := c.c.NewRequest(http.MethodPost, "/v1/auth/token/create")
|
2015-04-05 00:53:59 +00:00
|
|
|
if err := r.SetJSONBody(opts); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-03-23 21:47:43 +00:00
|
|
|
resp, err := c.c.rawRequestWithContext(ctx, r)
|
2015-04-05 00:53:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
return ParseSecret(resp.Body)
|
|
|
|
}
|
|
|
|
|
2016-09-01 19:39:44 +00:00
|
|
|
func (c *TokenAuth) CreateOrphan(opts *TokenCreateRequest) (*Secret, error) {
|
2022-03-23 21:47:43 +00:00
|
|
|
return c.CreateOrphanWithContext(context.Background(), opts)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *TokenAuth) CreateOrphanWithContext(ctx context.Context, opts *TokenCreateRequest) (*Secret, error) {
|
|
|
|
ctx, cancelFunc := c.c.withConfiguredTimeout(ctx)
|
|
|
|
defer cancelFunc()
|
|
|
|
|
2022-03-24 17:58:03 +00:00
|
|
|
r := c.c.NewRequest(http.MethodPost, "/v1/auth/token/create-orphan")
|
2016-09-01 19:39:44 +00:00
|
|
|
if err := r.SetJSONBody(opts); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-03-23 21:47:43 +00:00
|
|
|
resp, err := c.c.rawRequestWithContext(ctx, r)
|
2016-09-01 19:39:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
return ParseSecret(resp.Body)
|
|
|
|
}
|
|
|
|
|
2016-02-29 18:27:31 +00:00
|
|
|
func (c *TokenAuth) CreateWithRole(opts *TokenCreateRequest, roleName string) (*Secret, error) {
|
2022-03-23 21:47:43 +00:00
|
|
|
return c.CreateWithRoleWithContext(context.Background(), opts, roleName)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *TokenAuth) CreateWithRoleWithContext(ctx context.Context, opts *TokenCreateRequest, roleName string) (*Secret, error) {
|
|
|
|
ctx, cancelFunc := c.c.withConfiguredTimeout(ctx)
|
|
|
|
defer cancelFunc()
|
|
|
|
|
2022-03-24 17:58:03 +00:00
|
|
|
r := c.c.NewRequest(http.MethodPost, "/v1/auth/token/create/"+roleName)
|
2016-02-29 18:27:31 +00:00
|
|
|
if err := r.SetJSONBody(opts); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-03-23 21:47:43 +00:00
|
|
|
resp, err := c.c.rawRequestWithContext(ctx, r)
|
2016-02-29 18:27:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
return ParseSecret(resp.Body)
|
|
|
|
}
|
|
|
|
|
2015-12-29 20:18:59 +00:00
|
|
|
func (c *TokenAuth) Lookup(token string) (*Secret, error) {
|
2022-03-23 21:47:43 +00:00
|
|
|
return c.LookupWithContext(context.Background(), token)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *TokenAuth) LookupWithContext(ctx context.Context, token string) (*Secret, error) {
|
|
|
|
ctx, cancelFunc := c.c.withConfiguredTimeout(ctx)
|
|
|
|
defer cancelFunc()
|
|
|
|
|
2022-03-24 17:58:03 +00:00
|
|
|
r := c.c.NewRequest(http.MethodPost, "/v1/auth/token/lookup")
|
2016-08-24 19:59:43 +00:00
|
|
|
if err := r.SetJSONBody(map[string]interface{}{
|
|
|
|
"token": token,
|
|
|
|
}); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-12-29 20:18:59 +00:00
|
|
|
|
2022-03-23 21:47:43 +00:00
|
|
|
resp, err := c.c.rawRequestWithContext(ctx, r)
|
2016-03-10 20:09:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
return ParseSecret(resp.Body)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *TokenAuth) LookupAccessor(accessor string) (*Secret, error) {
|
2022-03-23 21:47:43 +00:00
|
|
|
return c.LookupAccessorWithContext(context.Background(), accessor)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *TokenAuth) LookupAccessorWithContext(ctx context.Context, accessor string) (*Secret, error) {
|
|
|
|
ctx, cancelFunc := c.c.withConfiguredTimeout(ctx)
|
|
|
|
defer cancelFunc()
|
|
|
|
|
2022-03-24 17:58:03 +00:00
|
|
|
r := c.c.NewRequest(http.MethodPost, "/v1/auth/token/lookup-accessor")
|
2016-08-24 19:59:43 +00:00
|
|
|
if err := r.SetJSONBody(map[string]interface{}{
|
|
|
|
"accessor": accessor,
|
|
|
|
}); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-07-24 22:49:55 +00:00
|
|
|
|
2022-03-23 21:47:43 +00:00
|
|
|
resp, err := c.c.rawRequestWithContext(ctx, r)
|
2015-12-29 20:18:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
return ParseSecret(resp.Body)
|
|
|
|
}
|
|
|
|
|
2015-10-30 21:27:33 +00:00
|
|
|
func (c *TokenAuth) LookupSelf() (*Secret, error) {
|
2022-03-23 21:47:43 +00:00
|
|
|
return c.LookupSelfWithContext(context.Background())
|
|
|
|
}
|
2015-10-30 21:27:33 +00:00
|
|
|
|
2022-03-23 21:47:43 +00:00
|
|
|
func (c *TokenAuth) LookupSelfWithContext(ctx context.Context) (*Secret, error) {
|
|
|
|
ctx, cancelFunc := c.c.withConfiguredTimeout(ctx)
|
2018-07-24 22:49:55 +00:00
|
|
|
defer cancelFunc()
|
2022-03-23 21:47:43 +00:00
|
|
|
|
2022-03-24 17:58:03 +00:00
|
|
|
r := c.c.NewRequest(http.MethodGet, "/v1/auth/token/lookup-self")
|
2022-03-23 21:47:43 +00:00
|
|
|
|
|
|
|
resp, err := c.c.rawRequestWithContext(ctx, r)
|
2015-10-30 21:27:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
return ParseSecret(resp.Body)
|
|
|
|
}
|
|
|
|
|
2019-11-08 16:32:01 +00:00
|
|
|
func (c *TokenAuth) RenewAccessor(accessor string, increment int) (*Secret, error) {
|
2022-03-23 21:47:43 +00:00
|
|
|
return c.RenewAccessorWithContext(context.Background(), accessor, increment)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *TokenAuth) RenewAccessorWithContext(ctx context.Context, accessor string, increment int) (*Secret, error) {
|
|
|
|
ctx, cancelFunc := c.c.withConfiguredTimeout(ctx)
|
|
|
|
defer cancelFunc()
|
|
|
|
|
2022-03-24 17:58:03 +00:00
|
|
|
r := c.c.NewRequest(http.MethodPost, "/v1/auth/token/renew-accessor")
|
2019-11-08 16:32:01 +00:00
|
|
|
if err := r.SetJSONBody(map[string]interface{}{
|
|
|
|
"accessor": accessor,
|
|
|
|
"increment": increment,
|
|
|
|
}); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-03-23 21:47:43 +00:00
|
|
|
resp, err := c.c.rawRequestWithContext(ctx, r)
|
2019-11-08 16:32:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
return ParseSecret(resp.Body)
|
|
|
|
}
|
|
|
|
|
2015-04-20 01:04:01 +00:00
|
|
|
func (c *TokenAuth) Renew(token string, increment int) (*Secret, error) {
|
2022-03-23 21:47:43 +00:00
|
|
|
return c.RenewWithContext(context.Background(), token, increment)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *TokenAuth) RenewWithContext(ctx context.Context, token string, increment int) (*Secret, error) {
|
|
|
|
ctx, cancelFunc := c.c.withConfiguredTimeout(ctx)
|
|
|
|
defer cancelFunc()
|
|
|
|
|
2022-03-24 17:58:03 +00:00
|
|
|
r := c.c.NewRequest(http.MethodPut, "/v1/auth/token/renew")
|
2016-08-24 19:59:43 +00:00
|
|
|
if err := r.SetJSONBody(map[string]interface{}{
|
|
|
|
"token": token,
|
|
|
|
"increment": increment,
|
|
|
|
}); err != nil {
|
2015-04-20 01:04:01 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-03-23 21:47:43 +00:00
|
|
|
resp, err := c.c.rawRequestWithContext(ctx, r)
|
2015-04-05 00:53:59 +00:00
|
|
|
if err != nil {
|
2015-04-20 01:04:01 +00:00
|
|
|
return nil, err
|
2015-04-05 00:53:59 +00:00
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2015-04-20 01:04:01 +00:00
|
|
|
return ParseSecret(resp.Body)
|
2015-04-05 00:53:59 +00:00
|
|
|
}
|
|
|
|
|
2015-10-30 21:27:33 +00:00
|
|
|
func (c *TokenAuth) RenewSelf(increment int) (*Secret, error) {
|
2022-03-23 21:47:43 +00:00
|
|
|
return c.RenewSelfWithContext(context.Background(), increment)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *TokenAuth) RenewSelfWithContext(ctx context.Context, increment int) (*Secret, error) {
|
|
|
|
ctx, cancelFunc := c.c.withConfiguredTimeout(ctx)
|
|
|
|
defer cancelFunc()
|
|
|
|
|
2022-03-24 17:58:03 +00:00
|
|
|
r := c.c.NewRequest(http.MethodPut, "/v1/auth/token/renew-self")
|
2015-10-30 21:27:33 +00:00
|
|
|
|
|
|
|
body := map[string]interface{}{"increment": increment}
|
|
|
|
if err := r.SetJSONBody(body); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-03-23 21:47:43 +00:00
|
|
|
resp, err := c.c.rawRequestWithContext(ctx, r)
|
2015-10-30 21:27:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
return ParseSecret(resp.Body)
|
|
|
|
}
|
|
|
|
|
2022-03-23 21:47:43 +00:00
|
|
|
// RenewTokenAsSelf wraps RenewTokenAsSelfWithContext using context.Background.
|
2017-06-20 22:52:40 +00:00
|
|
|
func (c *TokenAuth) RenewTokenAsSelf(token string, increment int) (*Secret, error) {
|
2022-03-23 21:47:43 +00:00
|
|
|
return c.RenewTokenAsSelfWithContext(context.Background(), token, increment)
|
|
|
|
}
|
|
|
|
|
|
|
|
// RenewTokenAsSelfWithContext behaves like renew-self, but authenticates using a provided
|
|
|
|
// token instead of the token attached to the client.
|
|
|
|
func (c *TokenAuth) RenewTokenAsSelfWithContext(ctx context.Context, token string, increment int) (*Secret, error) {
|
|
|
|
ctx, cancelFunc := c.c.withConfiguredTimeout(ctx)
|
|
|
|
defer cancelFunc()
|
|
|
|
|
2022-03-24 17:58:03 +00:00
|
|
|
r := c.c.NewRequest(http.MethodPut, "/v1/auth/token/renew-self")
|
2017-06-19 15:10:09 +00:00
|
|
|
r.ClientToken = token
|
|
|
|
|
|
|
|
body := map[string]interface{}{"increment": increment}
|
|
|
|
if err := r.SetJSONBody(body); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-03-23 21:47:43 +00:00
|
|
|
resp, err := c.c.rawRequestWithContext(ctx, r)
|
2017-06-19 15:10:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
return ParseSecret(resp.Body)
|
|
|
|
}
|
|
|
|
|
2022-03-23 21:47:43 +00:00
|
|
|
// RevokeAccessor wraps RevokeAccessorWithContext using context.Background.
|
2016-03-10 22:04:04 +00:00
|
|
|
func (c *TokenAuth) RevokeAccessor(accessor string) error {
|
2022-03-23 21:47:43 +00:00
|
|
|
return c.RevokeAccessorWithContext(context.Background(), accessor)
|
|
|
|
}
|
|
|
|
|
|
|
|
// RevokeAccessorWithContext revokes a token associated with the given accessor
|
|
|
|
// along with all the child tokens.
|
|
|
|
func (c *TokenAuth) RevokeAccessorWithContext(ctx context.Context, accessor string) error {
|
|
|
|
ctx, cancelFunc := c.c.withConfiguredTimeout(ctx)
|
|
|
|
defer cancelFunc()
|
|
|
|
|
2022-03-24 17:58:03 +00:00
|
|
|
r := c.c.NewRequest(http.MethodPost, "/v1/auth/token/revoke-accessor")
|
2016-08-24 19:59:43 +00:00
|
|
|
if err := r.SetJSONBody(map[string]interface{}{
|
|
|
|
"accessor": accessor,
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-07-24 22:49:55 +00:00
|
|
|
|
2022-03-23 21:47:43 +00:00
|
|
|
resp, err := c.c.rawRequestWithContext(ctx, r)
|
2015-04-05 00:53:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-03-23 21:47:43 +00:00
|
|
|
// RevokeOrphan wraps RevokeOrphanWithContext using context.Background.
|
2016-03-10 22:04:04 +00:00
|
|
|
func (c *TokenAuth) RevokeOrphan(token string) error {
|
2022-03-23 21:47:43 +00:00
|
|
|
return c.RevokeOrphanWithContext(context.Background(), token)
|
|
|
|
}
|
|
|
|
|
|
|
|
// RevokeOrphanWithContext revokes a token without revoking the tree underneath it (so
|
|
|
|
// child tokens are orphaned rather than revoked)
|
|
|
|
func (c *TokenAuth) RevokeOrphanWithContext(ctx context.Context, token string) error {
|
|
|
|
ctx, cancelFunc := c.c.withConfiguredTimeout(ctx)
|
|
|
|
defer cancelFunc()
|
|
|
|
|
2022-03-24 17:58:03 +00:00
|
|
|
r := c.c.NewRequest(http.MethodPut, "/v1/auth/token/revoke-orphan")
|
2016-08-24 19:59:43 +00:00
|
|
|
if err := r.SetJSONBody(map[string]interface{}{
|
|
|
|
"token": token,
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-03-23 21:47:43 +00:00
|
|
|
resp, err := c.c.rawRequestWithContext(ctx, r)
|
2015-04-05 00:53:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-03-23 21:47:43 +00:00
|
|
|
// RevokeSelf wraps RevokeSelfWithContext using context.Background.
|
|
|
|
func (c *TokenAuth) RevokeSelf(token string) error {
|
|
|
|
return c.RevokeSelfWithContext(context.Background(), token)
|
|
|
|
}
|
|
|
|
|
|
|
|
// RevokeSelfWithContext revokes the token making the call. The `token` parameter is kept
|
2016-09-13 15:03:05 +00:00
|
|
|
// for backwards compatibility but is ignored; only the client's set token has
|
|
|
|
// an effect.
|
2022-03-23 21:47:43 +00:00
|
|
|
func (c *TokenAuth) RevokeSelfWithContext(ctx context.Context, token string) error {
|
|
|
|
ctx, cancelFunc := c.c.withConfiguredTimeout(ctx)
|
|
|
|
defer cancelFunc()
|
|
|
|
|
2022-03-24 17:58:03 +00:00
|
|
|
r := c.c.NewRequest(http.MethodPut, "/v1/auth/token/revoke-self")
|
2018-07-24 22:49:55 +00:00
|
|
|
|
2022-03-23 21:47:43 +00:00
|
|
|
resp, err := c.c.rawRequestWithContext(ctx, r)
|
2016-03-11 11:30:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-03-23 21:47:43 +00:00
|
|
|
// RevokeTree wraps RevokeTreeWithContext using context.Background.
|
|
|
|
func (c *TokenAuth) RevokeTree(token string) error {
|
|
|
|
return c.RevokeTreeWithContext(context.Background(), token)
|
|
|
|
}
|
|
|
|
|
|
|
|
// RevokeTreeWithContext is the "normal" revoke operation that revokes the given token and
|
2016-02-03 16:42:13 +00:00
|
|
|
// the entire tree underneath -- all of its child tokens, their child tokens,
|
|
|
|
// etc.
|
2022-03-23 21:47:43 +00:00
|
|
|
func (c *TokenAuth) RevokeTreeWithContext(ctx context.Context, token string) error {
|
|
|
|
ctx, cancelFunc := c.c.withConfiguredTimeout(ctx)
|
|
|
|
defer cancelFunc()
|
|
|
|
|
2022-03-24 17:58:03 +00:00
|
|
|
r := c.c.NewRequest(http.MethodPut, "/v1/auth/token/revoke")
|
2016-08-24 19:59:43 +00:00
|
|
|
if err := r.SetJSONBody(map[string]interface{}{
|
|
|
|
"token": token,
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-03-23 21:47:43 +00:00
|
|
|
resp, err := c.c.rawRequestWithContext(ctx, r)
|
2015-04-05 00:53:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// TokenCreateRequest is the options structure for creating a token.
|
|
|
|
type TokenCreateRequest struct {
|
2015-11-09 22:30:50 +00:00
|
|
|
ID string `json:"id,omitempty"`
|
|
|
|
Policies []string `json:"policies,omitempty"`
|
|
|
|
Metadata map[string]string `json:"meta,omitempty"`
|
|
|
|
Lease string `json:"lease,omitempty"`
|
|
|
|
TTL string `json:"ttl,omitempty"`
|
2016-06-08 18:49:48 +00:00
|
|
|
ExplicitMaxTTL string `json:"explicit_max_ttl,omitempty"`
|
2016-08-13 01:01:30 +00:00
|
|
|
Period string `json:"period,omitempty"`
|
2015-11-09 22:30:50 +00:00
|
|
|
NoParent bool `json:"no_parent,omitempty"`
|
|
|
|
NoDefaultPolicy bool `json:"no_default_policy,omitempty"`
|
|
|
|
DisplayName string `json:"display_name"`
|
|
|
|
NumUses int `json:"num_uses"`
|
2016-06-08 15:14:30 +00:00
|
|
|
Renewable *bool `json:"renewable,omitempty"`
|
2018-10-15 16:56:24 +00:00
|
|
|
Type string `json:"type"`
|
2019-07-01 09:39:54 +00:00
|
|
|
EntityAlias string `json:"entity_alias"`
|
2015-04-05 00:53:59 +00:00
|
|
|
}
|