hcl

package
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Mar 1, 2024 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const (
	EvalContextEnvVarKey = "env"
)

Variables

View Source
var (
	Functions = map[string]function.Function{
		"concat":       klib.ConcatFunc,
		"contains":     stdlib.ContainsFunc,
		"distinct":     stdlib.DistinctFunc,
		"haselement":   stdlib.SetHasElementFunc,
		"hasindex":     stdlib.HasIndexFunc,
		"hostname":     klib.HostnameFunc,
		"join":         stdlib.JoinFunc,
		"keys":         stdlib.KeysFunc,
		"lookup":       stdlib.LookupFunc,
		"lower":        stdlib.LowerFunc,
		"random":       klib.RandomFunc,
		"regex":        stdlib.RegexFunc,
		"replace":      stdlib.ReplaceFunc,
		"regexreplace": stdlib.RegexReplaceFunc,
		"sort":         stdlib.SortFunc,
		"split":        stdlib.SplitFunc,
		"strlen":       stdlib.StrlenFunc,
		"substr":       stdlib.SubstrFunc,
		"tomap":        stdlib.MakeToFunc(cty.Map(cty.DynamicPseudoType)),
		"trim":         stdlib.TrimFunc,
		"trimprefix":   stdlib.TrimPrefixFunc,
		"trimspace":    stdlib.TrimSpaceFunc,
		"trimsuffix":   stdlib.TrimSuffixFunc,
		"upper":        stdlib.UpperFunc,
		"values":       stdlib.ValuesFunc,
	}
)

ServiceTypes ...

Functions

func AssignAttributesToEvalContext

func AssignAttributesToEvalContext(ctx *hcl.EvalContext, body hcl.Body,
	options ...AssignAttributesToEvalContextOption) (map[string]string, error)

AssignAttributesToEvalContext picks up all attributes in a hcl.Body, evaluates their values, and assigns them to ctx. In case an attribute is "forced" to a map, e.g.,

forced-map-attribute = tomap({
    "key0": {a: "aaa", b: true }
    "key1": {a: "bbb", b: false }
})

the key-value pairs are merged into a pre-existing map, as long as their types match, otherwise an error is returned. Of course, to be processed at all, all elements within the map have to match as well. If the AssignAttributesToEvalContextRootKeyOption is used, rootKey is the key in hcl.EvalContext's Variables map all attributes are assigned to. This is aimed at, for example, setting up a common "namespace" for references, like "vars".

func AssignDecodedBlockToEvalCtxVars

func AssignDecodedBlockToEvalCtxVars(ctx *hcl.EvalContext, block *hcl.Block, t kcty.CtyMarshalable)

AssignDecodedBlockToEvalCtxVars is a convenience function extracting forwarding block's type and first label onto AssignToVars.

func AssignEnvVarsToEvalContext

func AssignEnvVarsToEvalContext(ctx *hcl.EvalContext, envVars ...string)

AssignEnvVarsToEvalContext assigns all envVars to the hcl.EvalContext ctx as a map under the key EvalContextEnvVarKey. envVars strings are expected to be "lines" of environment variables key/value assignments. Spaces and quotes of quoted values are ignored. If a line starts with a "#" the line is ignored entirely. An existing value for a key will be overridden.

func AssignToEvalCtxVars

func AssignToEvalCtxVars(ctx *hcl.EvalContext, typeString, label string, t kcty.CtyMarshalable)

AssignToEvalCtxVars unmarshals t into a cty.Value and assigns it as a variable in the context ctx using an hierarchical "key-path" starting with typeString and afterwards label, e.g., let T be of type RESTClient and its HCL configuration:

client service-provider-A {
  method = "POST"
	 url = "https://example.com"
}

then the corresponding URL value can be accessed by the reference expression client.service-provider-A.url

func AssignToEvalCtxVarsByDecodingBlock

func AssignToEvalCtxVarsByDecodingBlock[T kcty.CtyMarshalable](ctx *hcl.EvalContext, block *hcl.Block) error

AssignToEvalCtxVarsByDecodingBlock is a convenience function ignoring the decoded block as a return value and therefore making it easier to use as a registered function. NOTE the evaluation context ctx is going to be modified by this function

func AssignVariablesFromBlock

func AssignVariablesFromBlock(ctx *hcl.EvalContext, block *hcl.Block) error

AssignVariablesFromBlock looks up a block's type in the a global block-assigner registry, attempts to decode the block, marshals the decoded block to a cty.Value, and assigns it to ctx as a variable. NOTE the evaluation context ctx is going to be modified by this function

func DecodeBlock

func DecodeBlock[T any](ctx *hcl.EvalContext, block *hcl.Block) (t T, err error)

DecodeBlock is a "generics-wrapper" for gohcl's DecodeBody accepting a block argument. NOTE At this point labels, if there are any to begin with, have been already assigned to block during parsing of the HCL-configuration input. To assign the label-values to T's concrete fields, part of gohcl's decoding has to be replicated, see decodeBodyToStruct() and decodeBlockToValue() in in github.com/hashicorp/hcl/v2/gohcl/decode.go and getFieldTags() in github.com/hashicorp/hcl/v2/gohcl/schema.go.

func DecodeBlockAndAssignDecodedBlockToEvalCtxVars

func DecodeBlockAndAssignDecodedBlockToEvalCtxVars[T kcty.CtyMarshalable](ctx *hcl.EvalContext, block *hcl.Block) (t T, err error)

DecodeBlock decodes block, returns the a structure of type T, and adds a cty-encoded version of the decoded structure to the hcl.EvalContext ctx as a variable keyed by its block.Type identifier. NOTE the evaluation context ctx is going to be modified by this function

func DecodeBody

func DecodeBody[T any](ctx *hcl.EvalContext, body hcl.Body) (T, error)

DecodeBody is a simple "generics-wrapper" for gohcl's DecodeBody.

func DecodeBodyLabelReader

func DecodeBodyLabelReader[T any](ctx *hcl.EvalContext, reader BodyLabelReader) (t T, err error)

func NewContext

func NewContext(parent *hcl.EvalContext, options ...ContextOption) (*hcl.EvalContext, error)

NewContext creates a new hcl.EvalContext pre-populated with variables and functions that have registered with RegisterEvalContextVar and RegisterEvalContextFunc. If parent is not nil, a child context is derived where all variables and functions are inherited (and potentially subsequently overridden) from the parent context.

func RegisterAssignableBlock

func RegisterAssignableBlock[T kcty.CtyMarshalable](blockTypes ...string)

RegisterAssignableBlock registers a CtyMarshableType to be assignable as context variables.

func RegisterEvalContextFunc

func RegisterEvalContextFunc(key string, f function.Function)

RegisterEvalContextVar registers a hcl.EvalContext function which is going to be always included when constructing an context with NewContext.

func RegisterEvalContextVar

func RegisterEvalContextVar(key string, v cty.Value)

RegisterEvalContextVar registers a hcl.EvalContext variable which is going to be always included when constructing an context with NewContext.

Types

type APIGWClient

type APIGWClient = RESTClient

APIGWClient is a type alias for a RESTClient.

type AssignAttributesToEvalContextOption

type AssignAttributesToEvalContextOption func(*assignAttributesToEvalContextOptionArg)

An AssignAttributesToEvalContextOption configures the behaviour of AssignAttributesToEvalContext.

func AssignAttributesToEvalContextRootKeyOption

func AssignAttributesToEvalContextRootKeyOption(rootKey string) AssignAttributesToEvalContextOption

AssignAttributesToEvalContextRootKeyOption assigns a rootKey in hcl.EvalContext's Variables map to assign attributes to. This is aimed at, for example, setting up a common "namespace" for references, like "vars".

type BlockWrapper

type BlockWrapper hcl.Block

A BlockWrapper is a convenience structure enabling an hcl.Block's compliance with the BodyLabelReader interface.

func (*BlockWrapper) AllLabels

func (bw *BlockWrapper) AllLabels() []string

AllLabels implements the LabelReader interface.

func (*BlockWrapper) FirstLabel

func (bw *BlockWrapper) FirstLabel() string

FirstLabel implements the LabelReader interface.

func (*BlockWrapper) GetBody

func (bw *BlockWrapper) GetBody() hcl.Body

GetBody implements the BodyReader interface.

type BodyLabelReader

type BodyLabelReader interface {
	BodyReader
	LabelReader
}

BodyLabelReader is a combined interface.

type BodyReader

type BodyReader interface {
	GetBody() hcl.Body
}

BodyReader implementations are expected to return an HCL body.

type ContextOption

type ContextOption func(*hcl.EvalContext) error

A ContextOption allows to modify the context returned by NewContext during initialization.

func ContextDotEnvFileOption

func ContextDotEnvFileOption(dotEnvFile string) ContextOption

ContextDotEnvFileOption assigns envVars to the hcl.EvalContext under the root-key "env". NOTE the format for explicit env-vars is (value has to be in quotes!) name="value"

func ContextExplicitEnvVarsOption

func ContextExplicitEnvVarsOption(envVars ...string) ContextOption

ContextExplicitEnvVarsOption assigns envVars to the hcl.EvalContext under the root-key "env". NOTE the format for explicit env-vars is (value has to be in quotes!) name="value"

func ContextExplicitVarsOption

func ContextExplicitVarsOption(rootKey string, values map[string]cty.Value) ContextOption

ContextExplicitVarsOption assigns values to an hcl.EvalContext's Variables map. If key is empty, then values is assigned "on the root level", overriding existing assignments. Otherwise, a map "below the root level" is assumed, in which case the values map is merged into an existing one.

func ContextOSEnvVarsOption

func ContextOSEnvVarsOption() ContextOption

ContextOSEnvVarsOption assigns the runtime env vars to the hcl.EvalContext under the root-key "env".

type LabelReader

type LabelReader interface {
	// Label implementations are expected to return the first label of an
	//  HCL-block.
	FirstLabel() string
	// Labels implementations are expected to return a slice of labels for an
	//  HCL-block.
	AllLabels() []string
}

LabelReader implementations are expected to return the labels of a HCL-block.

type OIDCClient

type OIDCClient struct {
	ClientID     string              `hcl:"client_id,label" cty:"client_id"`
	AuthMethod   string              `hcl:"auth_method,optional" cty:"auth_method"`
	AuthToken    string              `hcl:"auth_token,optional" cty:"auth_token"`
	ClientSecret string              `hcl:"client_secret,optional" cty:"client_secret"`
	Endpoints    *OIDCEndpoints      `hcl:"endpoints,block" cty:"endpoints"`
	Headers      map[string][]string `hcl:"headers,optional" cty:"headers"`
	RedirectURIs []string            `hcl:"redirect_uris,optional" cty:"redirect_uris"`
	Scopes       []string            `hcl:"scopes,optional" cty:"scopes"`
	TLS          *TLS                `hcl:"tls,block" cty:"tls"`
}

OIDCClient contains settings pertaining to OIDC clients.

func (OIDCClient) AllLabels

func (oc OIDCClient) AllLabels() []string

AllLabels implements the LabelReader interface.

func (OIDCClient) CtyType

func (oc OIDCClient) CtyType() cty.Type

CtyType implements the CtyTypeable interface.

func (OIDCClient) CtyValue

func (oc OIDCClient) CtyValue() cty.Value

ToCtyValue implements the CtyMarshalable interface.

func (OIDCClient) FirstLabel

func (oc OIDCClient) FirstLabel() string

FirstLabel implements the LabelReader interface.

type OIDCEndpoints

type OIDCEndpoints struct {
	Auth          string `hcl:"auth,optional" cty:"auth"`
	Introspection string `hcl:"introspection,optional" cty:"introspection"`
	Issuer        string `hcl:"issuer" cty:"issuer"`
	JWKS          string `hcl:"jwks,optional" cty:"jwks"`
	Token         string `hcl:"token,optional" cty:"token"`
	Userinfo      string `hcl:"userinfo,optional" cty:"userinfo"`
}

OIDCEndpoints contains typical URLs pertaining to an OIDC provider.

func (OIDCEndpoints) CtyType

func (oe OIDCEndpoints) CtyType() cty.Type

CtyType implements the CtyTypeable interface.

func (OIDCEndpoints) CtyValue

func (oe OIDCEndpoints) CtyValue() cty.Value

ToCtyValue implements the CtyMarshalable interface.

type OIDCProvider

type OIDCProvider struct {
	Alias         string         `hcl:"alias,label" cty:"alias"`
	Endpoints     *OIDCEndpoints `hcl:"endpoints,block" cty:"endpoints"`
	Clients       []*OIDCClient  `hcl:"client,block" cty:"client"`
	DefaultClient string         `hcl:"default_client,optional" cty:"default_client"`
}

OIDCProvider contains settings pertaining to OIDC providers.

func (OIDCProvider) AllLabels

func (op OIDCProvider) AllLabels() []string

AllLabels implements the LabelReader interface.

func (OIDCProvider) CtyType

func (op OIDCProvider) CtyType() cty.Type

CtyType implements the CtyTypeable interface.

func (OIDCProvider) CtyValue

func (op OIDCProvider) CtyValue() cty.Value

ToCtyValue implements the CtyMarshalable interface.

func (OIDCProvider) FirstLabel

func (op OIDCProvider) FirstLabel() string

FirstLabel implements the LabelReader interface.

type RESTClient

type RESTClient struct {
	Alias      string              `hcl:"alias,label" cty:"alias"`
	AuthMethod string              `hcl:"auth_method,optional" cty:"auth_method"`
	AuthToken  string              `hcl:"auth_token,optional" cty:"auth_token"`
	Headers    map[string][]string `hcl:"headers,optional" cty:"headers"`
	TLS        *TLS                `hcl:"tls,block" cty:"tls"`
	URL        string              `hcl:"url,optional" cty:"url"`
}

RESTClient contains settings pertaining to REST clients.

func (RESTClient) AllLabels

func (rc RESTClient) AllLabels() []string

AllLabels implements the LabelReader interface.

func (RESTClient) CtyType

func (rc RESTClient) CtyType() cty.Type

CtyType implements the CtyTypeable interface.

func (RESTClient) CtyValue

func (rc RESTClient) CtyValue() cty.Value

ToCtyValue implements the CtyMarshalable interface.

func (RESTClient) FirstLabel

func (rc RESTClient) FirstLabel() string

FirstLabel implements the LabelReader interface.

type ServiceType

type ServiceType = string

ServiceType distinguishes different services, which can be decoded

const (
	ServiceTypeAPIGW ServiceType = "apigw"
	ServiceTypeOIDC  ServiceType = "oidc"
	ServiceTypeREST  ServiceType = "rest"
)

ServiceType values

type TLS

type TLS struct {
	// ClientAuthType determines the server's policy for
	// TLS Client Authentication. The default is NoClientCert.
	ClientAuthType tls.ClientAuthType `hcl:"client_auth_type,optional" cty:"client_auth_type"`
	// ClientCAs defines the set of root certificate authorities that servers
	// expect to be used use when verifying a client certificate (if so
	// configured by the policy in ClientAuthType.
	ClientCAs []string `hcl:"client_cas,optional" cty:"client_cas"`
	// CurvePreferences contains the elliptic curves that will be used in
	// an ECDHE handshake, in preference order. If empty, the default will
	// be used. The client will use the first preference as the type for
	// its key share in TLS 1.3. This may change in the future.
	CurvePreferences []tls.CurveID `hcl:"curve_preferences,optional" cty:"curve_preferences"`
	// RootCAs defines the set of root certificate authorities that clients use
	// when verifying server certificates. If RootCAs is nil, TLS uses
	//  the host's root CA set.
	RootCAs []string `hcl:"root_cas,optional" cty:"root_cas"`
	// ServerName is used to verify the hostname on the returned
	// certificates unless InsecureSkipVerify is given. It is also included
	// in the client's handshake to support virtual hosting unless it is
	// an IP address.
	ServerName string `hcl:"server_name,optional" cty:"server_name"`
	// MaxVersion contains the maximum TLS version that is acceptable.
	//
	// By default, the maximum version supported by this package is used,
	// which is currently TLS 1.3.
	MaxVersion uint16 `hcl:"max_version,optional" cty:"max_version"`
	// MinVersion contains the minimum TLS version that is acceptable.
	//
	// By default, TLS 1.2 is currently used as the minimum when acting as a
	// client, and TLS 1.0 when acting as a server. TLS 1.0 is the minimum
	// supported by this package, both as a client and as a server.
	//
	// The client-side default can temporarily be reverted to TLS 1.0 by
	// including the value "x509sha1=1" in the GODEBUG environment variable.
	// Note that this option will be removed in Go 1.19 (but it will still be
	// possible to set this field to VersionTLS10 explicitly).
	MinVersion uint16 `hcl:"min_version,optional" cty:"min_version"`
	// InsecureSkipVerify controls whether a client verifies the server's
	// certificate chain and host name. If InsecureSkipVerify is true, crypto/tls
	// accepts any certificate presented by the server and any host name in that
	// certificate. In this mode, TLS is susceptible to machine-in-the-middle
	// attacks unless custom verification is used. This should be used only for
	// testing or in combination with VerifyConnection or VerifyPeerCertificate.
	InsecureSkipVerify bool `hcl:"insecure_skip_verify,optional" cty:"insecure_skip_verify"`
}

TLS contains settings pertaining to a TLS socket configuration.

func (TLS) CtyType

func (t TLS) CtyType() cty.Type

CtyType implements the CtyTypeable interface.

func (TLS) CtyValue

func (t TLS) CtyValue() cty.Value

CtyValue implements the CtyMarshalable interface.

type TLSClientAuthTypeWrapper

type TLSClientAuthTypeWrapper tls.ClientAuthType

TLSClientAuthTypeWrapper is a convenience wrapper for tls.ClientAuthType.

func (TLSClientAuthTypeWrapper) CtyValue

func (t TLSClientAuthTypeWrapper) CtyValue() cty.Value

CtyValue implements the CtyMarshaler interface.

func (TLSClientAuthTypeWrapper) String

func (t TLSClientAuthTypeWrapper) String() string

String implements the fmt.Stringer interface

type TLSCurveIDWrapper

type TLSCurveIDWrapper kcty.UInt16Wrapper

TLSCurveIDWrapper is a ceonvenience wrapper for tls.CurveID.

func (TLSCurveIDWrapper) CtyValue

func (t TLSCurveIDWrapper) CtyValue() cty.Value

CtyValue implements the CtyMarshaler interface.

func (TLSCurveIDWrapper) String

func (t TLSCurveIDWrapper) String() string

String implements the fmt.Stringer interface

type TLSCurvePreferences

type TLSCurvePreferences []tls.CurveID

TLSCurvePreferences is a convenience wrapper for []tls.CurveID.

func (TLSCurvePreferences) CtyValue

func (t TLSCurvePreferences) CtyValue() cty.Value

CtyValue implements the CtyMarshaler interface.

type TLSVersionWrapper

type TLSVersionWrapper kcty.UInt16Wrapper

TLSVersionWrapper is a convenience wrapper for TLS versions.

func (TLSVersionWrapper) CtyValue

func (t TLSVersionWrapper) CtyValue() cty.Value

CtyValue implements the CtyMarshaler interface.

func (TLSVersionWrapper) String

func (t TLSVersionWrapper) String() string

String implements the fmt.Stringer interface

Jump to

Keyboard shortcuts

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