base

package
v0.11.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 2, 2024 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Overview

Package base contains functions to add and retrieve auth from context

Index

Constants

View Source
const (
	OAuth2KeyAccessToken = "accessToken"

	AuthHeaderAuthorization = "Authorization"

	AuthPrefixBearer = "Bearer"

	// OAuth2ClientIDKey is the key of the clientID for AuthTypeOAuth2 secrets
	OAuth2ClientIDKey = "clientID"
	// OAuth2ClientSecretKey is the key of the clientSecret for AuthTypeOAuth2 secrets
	OAuth2ClientSecretKey = "clientSecret"
	// OAuth2CodeKey is the key of the code for AuthTypeOAuth2 secrets
	OAuth2CodeKey = "code"
	// OAuth2AccessTokenKeyKey is the key of the accessTokenKey for AuthTypeOAuth2 secrets
	OAuth2AccessTokenKeyKey = "accessTokenKey"
	// OAuth2AccessTokenKey is the key of the accessToken for AuthTypeOAuth2 secrets
	OAuth2AccessTokenKey = "accessToken"
	// OAuth2ScopeKey is the key of the scope for AuthTypeOAuth2 secrets
	OAuth2ScopeKey = "scope"
	// OAuth2RefreshTokenKey is the key of the refreshToken for AuthTypeOAuth2 secrets
	OAuth2RefreshTokenKey = "refreshToken"
	// OAuth2ExpiresInKey is the key of the expiresIn for AuthTypeOAuth2 secrets
	OAuth2CreatedAtKey = "createdAt"
	// OAuth2ExpiresInKey is the key of the expiresIn for AuthTypeOAuth2 secrets
	OAuth2ExpiresInKey = "expiresIn"
	// OAuth2RedirectURLKey is the key of the redirectURL for AuthTypeOAuth2 secrets
	OAuth2RedirectURLKey = "redirectURL"
	// OAuth2BaseURLKey is the key of the baseURL for AuthTypeOAuth2 secrets
	OAuth2BaseURLKey = "baseURL"

	// DynamicUsernameKey is the key of the username for  dynamic secrets.
	DynamicUsernameKey = corev1.BasicAuthUsernameKey
	// DynamicPasswordKey is the key of the password for  dynamic secrets.
	DynamicPasswordKey = corev1.BasicAuthPasswordKey

	// DynamicClientKeyKey is the key of the clientKey for dynamic secret
	DynamicClientKeyKey = "key"
	// DynamicClientSecretKey is the key of the clientSecret for dynamic secret
	DynamicClientSecretKey = "secret"
	// redefine key for dynamic token reflush.
	DynamicAccessTokenKey  = OAuth2AccessTokenKey
	DynamicRefreshTokenKey = OAuth2RefreshTokenKey
	DynamicCreatedAtKey    = OAuth2CreatedAtKey
	DynamicExpiresInKey    = OAuth2ExpiresInKey
	DynamicBaseURLKey      = OAuth2BaseURLKey
)
View Source
const (
	// PluginAuthHeader header for auth type (kubernetes secret type)
	PluginAuthHeader = "X-Plugin-Auth"
	// PluginSecretHeader header to store data part of the secret
	PluginSecretHeader = "X-Plugin-Secret"
)
View Source
const (
	// PluginMetaHeader header to store metadata for the plugin
	PluginMetaHeader = "X-Plugin-Meta"

	// PluginSubresourcesHeader subresources header parameter
	// used as a header to avoid overloading the url query parameters
	// and any url length limits
	PluginSubresourcesHeader = "X-Subresources"
)

Variables

This section is empty.

Functions

func AuthFilter

func AuthFilter(req *restful.Request, resp *restful.Response, chain *restful.FilterChain)

AuthFilter auth filter for go restful, parsing plugin auth

func GetSubResourcesOptionsFromRequest

func GetSubResourcesOptionsFromRequest(req *restful.Request) (opts metav1alpha1.SubResourcesOptions)

GetSubResourcesOptionsFromRequest returns SubResourcesOptions based on a request

func IsNotImplementedError

func IsNotImplementedError(err error) bool

IsNotImplementedError returns true if the plugin not implement the specified interface

func MetaFilter

func MetaFilter(req *restful.Request, resp *restful.Response, chain *restful.FilterChain)

MetaFilter meta filter for go restful, parsing plugin meta

Types

type Auth

type Auth struct {
	// Type secret type as in kubernetes secret.type
	Type v1alpha1.AuthType `json:"type"`
	// Secret 's data value extracted from kubernetes
	Secret map[string][]byte `json:"data"`
}

Auth plugin auth

func AuthFromRequest

func AuthFromRequest(req *restful.Request) (*Auth, error)

func ExtractAuth

func ExtractAuth(ctx context.Context) *Auth

ExtractAuth extract auth from a specific context

func FromSecret

func FromSecret(secret corev1.Secret) *Auth

FromSecret generate auth from secret

func (*Auth) Basic

func (a *Auth) Basic() (AuthMethod, error)

Basic return a Basic auth function

func (*Auth) BearerToken

func (a *Auth) BearerToken(attribute string) (AuthMethod, error)

BearerToken return an bearer token auth method

func (*Auth) Get

func (a *Auth) Get(attribute string) (string, error)

Get get specific attribute from secret

func (*Auth) GetBasicInfo

func (a *Auth) GetBasicInfo() (userName string, password string, err error)

GetBasicInfo get basic auth username and password

func (*Auth) GetDynamicInfo

func (a *Auth) GetDynamicInfo() (ak string, sk string, err error)

GetDynamicInfo get dynamic auth ak and sk

func (*Auth) GetOAuth2Token

func (a *Auth) GetOAuth2Token() (string, error)

GetOAuth2Token get oauth2 access token

func (*Auth) Header

func (a *Auth) Header(attribute string, header string) (AuthMethod, error)

Header return an auth method which could append to header with specific attribute

func (*Auth) HeaderWithPrefix

func (a *Auth) HeaderWithPrefix(attribute string, header string, prefix string) (AuthMethod, error)

HeaderWithPrefix return an auth method which could append to header with specific attribute and prefix

func (*Auth) IsBasic

func (a *Auth) IsBasic() bool

IsBasic check auth is basic

func (*Auth) IsDynamic

func (a *Auth) IsDynamic() bool

IsDynamic check auth is dynamic

func (*Auth) IsOAuth2

func (a *Auth) IsOAuth2() bool

IsOAuth2 check auth is oauth2

func (*Auth) OAuth2

func (a *Auth) OAuth2() (AuthMethod, error)

OAuth2 return an oauth2 auth method

func (*Auth) Query

func (a *Auth) Query(attribute string, query string) (AuthMethod, error)

Query return an auth method which could append to query with specific attribute

func (*Auth) WithContext

func (a *Auth) WithContext(ctx context.Context) context.Context

WithContext returns a copy of parent include with the auth

type AuthMethod

type AuthMethod func(request *resty.Request)

type BuildOptions

type BuildOptions func(client *PluginClient)

BuildOptions Options to build the plugin client

func ClientOpts

func ClientOpts(clt *resty.Client) BuildOptions

ClientOpts adds a custom client build options for plugin client

type Client

type Client interface {
	Get(ctx context.Context, baseURL *duckv1.Addressable, uri string, options ...OptionFunc) error
	GetResponse(ctx context.Context, baseURL *duckv1.Addressable, uri string, options ...OptionFunc) (*resty.Response, error)
	Post(ctx context.Context, baseURL *duckv1.Addressable, uri string, options ...OptionFunc) error
	Put(ctx context.Context, baseURL *duckv1.Addressable, uri string, options ...OptionFunc) error
	Delete(ctx context.Context, baseURL *duckv1.Addressable, uri string, options ...OptionFunc) error
}

Client interface for PluginClient, client code should use the interface as dependency

type Meta

type Meta struct {
	Version string `json:"version,omitempty"`
	BaseURL string `json:"baseURL,omitempty"`
}

Meta Plugin meta with base url and version info, for calling plugin api

func ExtraMeta

func ExtraMeta(ctx context.Context) *Meta

ExtraMeta extract meta from a specific context

func MetaFromRequest

func MetaFromRequest(req *restful.Request) (*Meta, error)

func (*Meta) WithContext

func (p *Meta) WithContext(ctx context.Context) context.Context

WithContext returns a copy of parent include with the plugin meta

type OptionFunc

type OptionFunc func(request *resty.Request)

OptionFunc options for requests

func BodyOpts

func BodyOpts(body interface{}) OptionFunc

BodyOpts request body

func DefaultOptions

func DefaultOptions() []OptionFunc

DefaultOptions for default plugin client options

func DoNotParseResponseOpts

func DoNotParseResponseOpts() OptionFunc

DoNotParseResponseOpts do not parse response

func ErrorOpts

func ErrorOpts(err interface{}) OptionFunc

ErrorOpts error response object

func HeaderOpts

func HeaderOpts(key, value string) OptionFunc

HeaderOpts sets a header

func ListOpts

func ListOpts(opts metav1alpha1.ListOptions) OptionFunc

ListOpts options for lists

func MetaOpts

func MetaOpts(meta Meta) OptionFunc

MetaOpts provides metadata for the request

func QueryOpts

func QueryOpts(params map[string]string) OptionFunc

QueryOpts query parameters for the request

func ResultOpts

func ResultOpts(dest interface{}) OptionFunc

ResultOpts request result automatically marshalled into object

func SecretOpts

func SecretOpts(secret corev1.Secret) OptionFunc

SecretOpts provides a secret to be assigned to the request in the header

func SubResourcesOpts

func SubResourcesOpts(subResources []string) OptionFunc

SubResourcesOpts set subresources header for the request

type PluginClient

type PluginClient struct {

	// ClassAddress is the address of the integration class
	// +optional
	ClassAddress *duckv1.Addressable
	// contains filtered or unexported fields
}

PluginClient client for plugins

func NewPluginClient

func NewPluginClient(opts ...BuildOptions) *PluginClient

NewPluginClient creates a new plugin client

func (*PluginClient) Clone

func (p *PluginClient) Clone() *PluginClient

Clone shallow clone the plugin client used to update some fields without changing the original

func (*PluginClient) Delete

func (p *PluginClient) Delete(ctx context.Context, baseURL *duckv1.Addressable, path string, options ...OptionFunc) error

Delete performs a DELETE request with the given parameters

func (*PluginClient) FullUrl

func (p *PluginClient) FullUrl(address *duckv1.Addressable, uri string) string

func (*PluginClient) Get

func (p *PluginClient) Get(ctx context.Context, baseURL *duckv1.Addressable, path string, options ...OptionFunc) error

Get performs a GET request using defined options

func (*PluginClient) GetMeta

func (p *PluginClient) GetMeta() Meta

func (*PluginClient) GetResponse

func (p *PluginClient) GetResponse(ctx context.Context, baseURL *duckv1.Addressable, path string, options ...OptionFunc) (*resty.Response, error)

GetResponse performs a GET request using defined options and return response

func (*PluginClient) GetSecret

func (p *PluginClient) GetSecret() corev1.Secret

func (*PluginClient) HandleError

func (p *PluginClient) HandleError(response *resty.Response, err error) error

func (*PluginClient) Post

func (p *PluginClient) Post(ctx context.Context, baseURL *duckv1.Addressable, path string, options ...OptionFunc) error

Post performs a POST request with the given parameters

func (*PluginClient) Put

func (p *PluginClient) Put(ctx context.Context, baseURL *duckv1.Addressable, path string, options ...OptionFunc) error

Put performs a PUT request with the given parameters

func (*PluginClient) R

func (p *PluginClient) R(ctx context.Context, options ...OptionFunc) *resty.Request

R prepares a request based on the given information

func (*PluginClient) WithClassAddress

func (p *PluginClient) WithClassAddress(classAddress *duckv1.Addressable) *PluginClient

func (*PluginClient) WithMeta

func (p *PluginClient) WithMeta(meta Meta) *PluginClient

func (*PluginClient) WithRequestOptions

func (p *PluginClient) WithRequestOptions(opts ...OptionFunc) *PluginClient

WithRequestOptions set request options

func (*PluginClient) WithSecret

func (p *PluginClient) WithSecret(secret corev1.Secret) *PluginClient

type ResponseStatusErr

type ResponseStatusErr struct {
	metav1.Status
}

ResponseStatusErr is an error with `Status` type, used to handle plugin response

func (ResponseStatusErr) Error

func (p ResponseStatusErr) Error() string

Error implements error interface, output the error message

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL