2023-03-15 19:00:52 +03:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2015-04-05 03:53:59 +03:00
|
|
|
package api
|
|
|
|
|
2021-12-21 20:46:56 +03:00
|
|
|
import (
|
|
|
|
"context"
|
2022-03-24 20:58:03 +03:00
|
|
|
"net/http"
|
2021-12-21 20:46:56 +03:00
|
|
|
)
|
2018-07-25 01:49:55 +03:00
|
|
|
|
2016-04-05 18:00:12 +03:00
|
|
|
// TokenAuth is used to perform token backend operations on Vault
|
2015-04-05 03:53:59 +03:00
|
|
|
type TokenAuth struct {
|
|
|
|
c *Client
|
|
|
|
}
|
|
|
|
|
2016-04-05 18:00:12 +03:00
|
|
|
// Token is used to return the client for token-backend API calls
|
2015-04-05 03:54:16 +03:00
|
|
|
func (a *Auth) Token() *TokenAuth {
|
2015-04-05 03:53:59 +03:00
|
|
|
return &TokenAuth{c: a.c}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *TokenAuth) Create(opts *TokenCreateRequest) (*Secret, error) {
|
2022-03-24 00:47:43 +03: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 20:58:03 +03:00
|
|
|
r := c.c.NewRequest(http.MethodPost, "/v1/auth/token/create")
|
2015-04-05 03:53:59 +03:00
|
|
|
if err := r.SetJSONBody(opts); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-03-24 00:47:43 +03:00
|
|
|
resp, err := c.c.rawRequestWithContext(ctx, r)
|
2015-04-05 03:53:59 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
return ParseSecret(resp.Body)
|
|
|
|
}
|
|
|
|
|
2016-09-01 22:39:44 +03:00
|
|
|
func (c *TokenAuth) CreateOrphan(opts *TokenCreateRequest) (*Secret, error) {
|
2022-03-24 00:47:43 +03: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 20:58:03 +03:00
|
|
|
r := c.c.NewRequest(http.MethodPost, "/v1/auth/token/create-orphan")
|
2016-09-01 22:39:44 +03:00
|
|
|
if err := r.SetJSONBody(opts); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-03-24 00:47:43 +03:00
|
|
|
resp, err := c.c.rawRequestWithContext(ctx, r)
|
2016-09-01 22:39:44 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
return ParseSecret(resp.Body)
|
|
|
|
}
|
|
|
|
|
2016-02-29 21:27:31 +03:00
|
|
|
func (c *TokenAuth) CreateWithRole(opts *TokenCreateRequest, roleName string) (*Secret, error) {
|
2022-03-24 00:47:43 +03: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 20:58:03 +03:00
|
|
|
r := c.c.NewRequest(http.MethodPost, "/v1/auth/token/create/"+roleName)
|
2016-02-29 21:27:31 +03:00
|
|
|
if err := r.SetJSONBody(opts); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-03-24 00:47:43 +03:00
|
|
|
resp, err := c.c.rawRequestWithContext(ctx, r)
|
2016-02-29 21:27:31 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
return ParseSecret(resp.Body)
|
|
|
|
}
|
|
|
|
|
2015-12-29 23:18:59 +03:00
|
|
|
func (c *TokenAuth) Lookup(token string) (*Secret, error) {
|
2022-03-24 00:47:43 +03: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 20:58:03 +03:00
|
|
|
r := c.c.NewRequest(http.MethodPost, "/v1/auth/token/lookup")
|
2016-08-24 22:59:43 +03:00
|
|
|
if err := r.SetJSONBody(map[string]interface{}{
|
|
|
|
"token": token,
|
|
|
|
}); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-12-29 23:18:59 +03:00
|
|
|
|
2022-03-24 00:47:43 +03:00
|
|
|
resp, err := c.c.rawRequestWithContext(ctx, r)
|
2016-03-10 23:09:16 +03: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-24 00:47:43 +03: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 20:58:03 +03:00
|
|
|
r := c.c.NewRequest(http.MethodPost, "/v1/auth/token/lookup-accessor")
|
2016-08-24 22:59:43 +03:00
|
|
|
if err := r.SetJSONBody(map[string]interface{}{
|
|
|
|
"accessor": accessor,
|
|
|
|
}); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-07-25 01:49:55 +03:00
|
|
|
|
2022-03-24 00:47:43 +03:00
|
|
|
resp, err := c.c.rawRequestWithContext(ctx, r)
|
2015-12-29 23:18:59 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
return ParseSecret(resp.Body)
|
|
|
|
}
|
|
|
|
|
2015-10-31 00:27:33 +03:00
|
|
|
func (c *TokenAuth) LookupSelf() (*Secret, error) {
|
2022-03-24 00:47:43 +03:00
|
|
|
return c.LookupSelfWithContext(context.Background())
|
|
|
|
}
|
2015-10-31 00:27:33 +03:00
|
|
|
|
2022-03-24 00:47:43 +03:00
|
|
|
func (c *TokenAuth) LookupSelfWithContext(ctx context.Context) (*Secret, error) {
|
|
|
|
ctx, cancelFunc := c.c.withConfiguredTimeout(ctx)
|
2018-07-25 01:49:55 +03:00
|
|
|
defer cancelFunc()
|
2022-03-24 00:47:43 +03:00
|
|
|
|
2022-03-24 20:58:03 +03:00
|
|
|
r := c.c.NewRequest(http.MethodGet, "/v1/auth/token/lookup-self")
|
2022-03-24 00:47:43 +03:00
|
|
|
|
|
|
|
resp, err := c.c.rawRequestWithContext(ctx, r)
|
2015-10-31 00:27:33 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
return ParseSecret(resp.Body)
|
|
|
|
}
|
|
|
|
|
2019-11-08 19:32:01 +03:00
|
|
|
func (c *TokenAuth) RenewAccessor(accessor string, increment int) (*Secret, error) {
|
2022-03-24 00:47:43 +03: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 20:58:03 +03:00
|
|
|
r := c.c.NewRequest(http.MethodPost, "/v1/auth/token/renew-accessor")
|
2019-11-08 19:32:01 +03:00
|
|
|
if err := r.SetJSONBody(map[string]interface{}{
|
|
|
|
"accessor": accessor,
|
|
|
|
"increment": increment,
|
|
|
|
}); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-03-24 00:47:43 +03:00
|
|
|
resp, err := c.c.rawRequestWithContext(ctx, r)
|
2019-11-08 19:32:01 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
return ParseSecret(resp.Body)
|
|
|
|
}
|
|
|
|
|
2015-04-20 04:04:01 +03:00
|
|
|
func (c *TokenAuth) Renew(token string, increment int) (*Secret, error) {
|
2022-03-24 00:47:43 +03: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 20:58:03 +03:00
|
|
|
r := c.c.NewRequest(http.MethodPut, "/v1/auth/token/renew")
|
2016-08-24 22:59:43 +03:00
|
|
|
if err := r.SetJSONBody(map[string]interface{}{
|
|
|
|
"token": token,
|
|
|
|
"increment": increment,
|
|
|
|
}); err != nil {
|
2015-04-20 04:04:01 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-03-24 00:47:43 +03:00
|
|
|
resp, err := c.c.rawRequestWithContext(ctx, r)
|
2015-04-05 03:53:59 +03:00
|
|
|
if err != nil {
|
2015-04-20 04:04:01 +03:00
|
|
|
return nil, err
|
2015-04-05 03:53:59 +03:00
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2015-04-20 04:04:01 +03:00
|
|
|
return ParseSecret(resp.Body)
|
2015-04-05 03:53:59 +03:00
|
|
|
}
|
|
|
|
|
2015-10-31 00:27:33 +03:00
|
|
|
func (c *TokenAuth) RenewSelf(increment int) (*Secret, error) {
|
2022-03-24 00:47:43 +03: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 20:58:03 +03:00
|
|
|
r := c.c.NewRequest(http.MethodPut, "/v1/auth/token/renew-self")
|
2015-10-31 00:27:33 +03:00
|
|
|
|
|
|
|
body := map[string]interface{}{"increment": increment}
|
|
|
|
if err := r.SetJSONBody(body); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-03-24 00:47:43 +03:00
|
|
|
resp, err := c.c.rawRequestWithContext(ctx, r)
|
2015-10-31 00:27:33 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
return ParseSecret(resp.Body)
|
|
|
|
}
|
|
|
|
|
2022-03-24 00:47:43 +03:00
|
|
|
// RenewTokenAsSelf wraps RenewTokenAsSelfWithContext using context.Background.
|
2017-06-21 01:52:40 +03:00
|
|
|
func (c *TokenAuth) RenewTokenAsSelf(token string, increment int) (*Secret, error) {
|
2022-03-24 00:47:43 +03: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 20:58:03 +03:00
|
|
|
r := c.c.NewRequest(http.MethodPut, "/v1/auth/token/renew-self")
|
2017-06-19 18:10:09 +03:00
|
|
|
r.ClientToken = token
|
|
|
|
|
|
|
|
body := map[string]interface{}{"increment": increment}
|
|
|
|
if err := r.SetJSONBody(body); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-03-24 00:47:43 +03:00
|
|
|
resp, err := c.c.rawRequestWithContext(ctx, r)
|
2017-06-19 18:10:09 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
return ParseSecret(resp.Body)
|
|
|
|
}
|
|
|
|
|
2022-03-24 00:47:43 +03:00
|
|
|
// RevokeAccessor wraps RevokeAccessorWithContext using context.Background.
|
2016-03-11 01:04:04 +03:00
|
|
|
func (c *TokenAuth) RevokeAccessor(accessor string) error {
|
2022-03-24 00:47:43 +03: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 20:58:03 +03:00
|
|
|
r := c.c.NewRequest(http.MethodPost, "/v1/auth/token/revoke-accessor")
|
2016-08-24 22:59:43 +03:00
|
|
|
if err := r.SetJSONBody(map[string]interface{}{
|
|
|
|
"accessor": accessor,
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-07-25 01:49:55 +03:00
|
|
|
|
2022-03-24 00:47:43 +03:00
|
|
|
resp, err := c.c.rawRequestWithContext(ctx, r)
|
2015-04-05 03:53:59 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-03-24 00:47:43 +03:00
|
|
|
// RevokeOrphan wraps RevokeOrphanWithContext using context.Background.
|
2016-03-11 01:04:04 +03:00
|
|
|
func (c *TokenAuth) RevokeOrphan(token string) error {
|
2022-03-24 00:47:43 +03: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 20:58:03 +03:00
|
|
|
r := c.c.NewRequest(http.MethodPut, "/v1/auth/token/revoke-orphan")
|
2016-08-24 22:59:43 +03:00
|
|
|
if err := r.SetJSONBody(map[string]interface{}{
|
|
|
|
"token": token,
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-03-24 00:47:43 +03:00
|
|
|
resp, err := c.c.rawRequestWithContext(ctx, r)
|
2015-04-05 03:53:59 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-03-24 00:47:43 +03: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 18:03:05 +03:00
|
|
|
// for backwards compatibility but is ignored; only the client's set token has
|
|
|
|
// an effect.
|
2022-03-24 00:47:43 +03:00
|
|
|
func (c *TokenAuth) RevokeSelfWithContext(ctx context.Context, token string) error {
|
|
|
|
ctx, cancelFunc := c.c.withConfiguredTimeout(ctx)
|
|
|
|
defer cancelFunc()
|
|
|
|
|
2022-03-24 20:58:03 +03:00
|
|
|
r := c.c.NewRequest(http.MethodPut, "/v1/auth/token/revoke-self")
|
2018-07-25 01:49:55 +03:00
|
|
|
|
2022-03-24 00:47:43 +03:00
|
|
|
resp, err := c.c.rawRequestWithContext(ctx, r)
|
2016-03-11 14:30:45 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-03-24 00:47:43 +03: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 19:42:13 +03:00
|
|
|
// the entire tree underneath -- all of its child tokens, their child tokens,
|
|
|
|
// etc.
|
2022-03-24 00:47:43 +03:00
|
|
|
func (c *TokenAuth) RevokeTreeWithContext(ctx context.Context, token string) error {
|
|
|
|
ctx, cancelFunc := c.c.withConfiguredTimeout(ctx)
|
|
|
|
defer cancelFunc()
|
|
|
|
|
2022-03-24 20:58:03 +03:00
|
|
|
r := c.c.NewRequest(http.MethodPut, "/v1/auth/token/revoke")
|
2016-08-24 22:59:43 +03:00
|
|
|
if err := r.SetJSONBody(map[string]interface{}{
|
|
|
|
"token": token,
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-03-24 00:47:43 +03:00
|
|
|
resp, err := c.c.rawRequestWithContext(ctx, r)
|
2015-04-05 03:53:59 +03: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-10 01:30:50 +03: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 21:49:48 +03:00
|
|
|
ExplicitMaxTTL string `json:"explicit_max_ttl,omitempty"`
|
2016-08-13 04:01:30 +03:00
|
|
|
Period string `json:"period,omitempty"`
|
2015-11-10 01:30:50 +03: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 18:14:30 +03:00
|
|
|
Renewable *bool `json:"renewable,omitempty"`
|
2018-10-15 19:56:24 +03:00
|
|
|
Type string `json:"type"`
|
2019-07-01 12:39:54 +03:00
|
|
|
EntityAlias string `json:"entity_alias"`
|
2015-04-05 03:53:59 +03:00
|
|
|
}
|