2019-07-29 22:54:07 +00:00
|
|
|
package provisioner
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Method indicates the action to action that we will perform, it's used as part
|
|
|
|
// of the context in the call to authorize. It defaults to Sing.
|
|
|
|
type Method int
|
|
|
|
|
|
|
|
// The key to save the Method in the context.
|
|
|
|
type methodKey struct{}
|
|
|
|
|
|
|
|
const (
|
|
|
|
// SignMethod is the method used to sign X.509 certificates.
|
|
|
|
SignMethod Method = iota
|
2023-10-06 21:02:19 +00:00
|
|
|
// SignIdentityMethod is the method used to sign X.509 identity certificates.
|
|
|
|
SignIdentityMethod
|
2019-07-29 22:54:07 +00:00
|
|
|
// RevokeMethod is the method used to revoke X.509 certificates.
|
|
|
|
RevokeMethod
|
2019-12-20 21:30:05 +00:00
|
|
|
// RenewMethod is the method used to renew X.509 certificates.
|
|
|
|
RenewMethod
|
|
|
|
// SSHSignMethod is the method used to sign SSH certificates.
|
|
|
|
SSHSignMethod
|
|
|
|
// SSHRenewMethod is the method used to renew SSH certificates.
|
|
|
|
SSHRenewMethod
|
|
|
|
// SSHRevokeMethod is the method used to revoke SSH certificates.
|
|
|
|
SSHRevokeMethod
|
|
|
|
// SSHRekeyMethod is the method used to rekey SSH certificates.
|
|
|
|
SSHRekeyMethod
|
2019-07-29 22:54:07 +00:00
|
|
|
)
|
|
|
|
|
2019-10-28 18:50:43 +00:00
|
|
|
// String returns a string representation of the context method.
|
|
|
|
func (m Method) String() string {
|
|
|
|
switch m {
|
|
|
|
case SignMethod:
|
|
|
|
return "sign-method"
|
2023-10-06 21:02:19 +00:00
|
|
|
case SignIdentityMethod:
|
|
|
|
return "sign-identity-method"
|
2019-10-28 18:50:43 +00:00
|
|
|
case RevokeMethod:
|
|
|
|
return "revoke-method"
|
2019-12-20 21:30:05 +00:00
|
|
|
case RenewMethod:
|
|
|
|
return "renew-method"
|
|
|
|
case SSHSignMethod:
|
|
|
|
return "ssh-sign-method"
|
|
|
|
case SSHRenewMethod:
|
|
|
|
return "ssh-renew-method"
|
|
|
|
case SSHRevokeMethod:
|
|
|
|
return "ssh-revoke-method"
|
|
|
|
case SSHRekeyMethod:
|
|
|
|
return "ssh-rekey-method"
|
2019-10-28 18:50:43 +00:00
|
|
|
default:
|
|
|
|
return "unknown"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-29 22:54:07 +00:00
|
|
|
// NewContextWithMethod creates a new context from ctx and attaches method to
|
|
|
|
// it.
|
|
|
|
func NewContextWithMethod(ctx context.Context, method Method) context.Context {
|
|
|
|
return context.WithValue(ctx, methodKey{}, method)
|
|
|
|
}
|
|
|
|
|
2021-03-01 06:49:20 +00:00
|
|
|
// MethodFromContext returns the Method saved in ctx.
|
2019-07-29 22:54:07 +00:00
|
|
|
func MethodFromContext(ctx context.Context) Method {
|
|
|
|
m, _ := ctx.Value(methodKey{}).(Method)
|
|
|
|
return m
|
|
|
|
}
|
2022-05-20 21:41:44 +00:00
|
|
|
|
|
|
|
type tokenKey struct{}
|
|
|
|
|
|
|
|
// NewContextWithToken creates a new context with the given token.
|
|
|
|
func NewContextWithToken(ctx context.Context, token string) context.Context {
|
|
|
|
return context.WithValue(ctx, tokenKey{}, token)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TokenFromContext returns the token stored in the given context.
|
|
|
|
func TokenFromContext(ctx context.Context) (string, bool) {
|
|
|
|
token, ok := ctx.Value(tokenKey{}).(string)
|
|
|
|
return token, ok
|
|
|
|
}
|
2023-09-21 17:55:38 +00:00
|
|
|
|
|
|
|
// The key to save the certTypeKey in the context.
|
|
|
|
type certTypeKey struct{}
|
|
|
|
|
|
|
|
// NewContextWithCertType creates a new context with the given CertType.
|
|
|
|
func NewContextWithCertType(ctx context.Context, certType string) context.Context {
|
|
|
|
return context.WithValue(ctx, certTypeKey{}, certType)
|
|
|
|
}
|
|
|
|
|
|
|
|
// CertTypeFromContext returns the certType stored in the given context.
|
|
|
|
func CertTypeFromContext(ctx context.Context) (string, bool) {
|
|
|
|
certType, ok := ctx.Value(certTypeKey{}).(string)
|
|
|
|
return certType, ok
|
|
|
|
}
|