module

package
v0.0.20 Latest Latest
Warning

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

Go to latest
Published: Mar 21, 2023 License: MPL-2.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const (
	NodeLocationSelfKey   = "._key"
	NodeLocationSelfValue = "._value"
)
View Source
const CheckIdentityErrorMsg = "only allow \"a-z,A-Z,0-9,_\" and can't start with a number"
View Source
const DocSiteUrl = "http://selefra.io/docs"

Variables

View Source
var (
	ErrNotSupport = "not support operation"
)

Functions

func CheckIdentity

func CheckIdentity(s string) bool

func GetDefaultProviderConfigYamlConfiguration

func GetDefaultProviderConfigYamlConfiguration(providerName, providerVersion string) string

GetDefaultProviderConfigYamlConfiguration If the provider is not configured, this is the default configuration

func MergeBlockWithDiagnostics

func MergeBlockWithDiagnostics[T Block](blockA, blockB T, diagnostics *schema.Diagnostics) T

MergeBlockWithDiagnostics Merge two blocks

func ParseDuration

func ParseDuration(d string) (time.Duration, error)

ParseDuration

func ParseDuration(d string) (time.Duration, error) {
	d = strings.TrimSpace(d)
	dr, err := time.ParseDuration(d)
	if err == nil {
		return dr, nil
	}
	if strings.Contains(d, "d") {
		index := strings.Index(d, "d")
		hour, err := strconv.Atoi(d[:index])
		if err != nil {
			return dr, err
		}
		dr = time.Hour * 24 * time.Duration(hour)
		s := d[index+1:]
		if s != "" {
			ndr, err := time.ParseDuration(d[index+1:])
			if err != nil {
				return dr, err
			}
			dr += ndr
		}
		return dr, nil
	}

	dv, err := strconv.ParseInt(d, 10, 64)
	return time.Duration(dv), err
}

func RenderErrorTemplate

func RenderErrorTemplate(errorType string, location *NodeLocation) string

RenderErrorTemplate Output Example:

error[E827890]: syntax error, do not support modules[1].output

 -->  test_data\test.yaml:83:7 ( modules[1].output )
| 78   - name: example_module
| 79     uses: ./rules/
| 80     input:
| 81       name: selefra
| 82     output:
| 83       - "This is a test output message, resource region is {{.region}}."
|          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| 84
| 85 variables:
| 86   - key: test
| 87     default:

Types

type Block

type Block interface {

	// Validator block should be able to check that it's configuration is correct
	Validator

	// Locatable every block should be addressable, so you have to be able to figure out where the text is in that block
	// yaml to module parser fills in the location of the Block, so you can get the original location and content of the Block when you need it
	// The location information should not change and should be fixed once parsed
	Locatable

	// IsEmpty Determines whether the block is empty
	IsEmpty() bool
}

Block each block should implement this interface

type CloudBlock

type CloudBlock struct {

	// Which project in the cloud is associated with
	Project string `yaml:"project,omitempty" mapstructure:"project,omitempty"`

	//
	Organization string `yaml:"organization,omitempty" mapstructure:"organization,omitempty"`

	// Debug parameters, temporarily masked
	HostName string `yaml:"hostname,omitempty" mapstructure:"hostname,omitempty"`

	*LocatableImpl `yaml:"-"`
}

CloudBlock CloudBlock-related configuration

func NewCloudBlock

func NewCloudBlock() *CloudBlock

func (*CloudBlock) Check

func (x *CloudBlock) Check(module *Module, validatorContext *ValidatorContext) *schema.Diagnostics

func (*CloudBlock) IsEmpty

func (x *CloudBlock) IsEmpty() bool

type ConnectionBlock

type ConnectionBlock struct {
	// These params are mutually exclusive with DSN
	Type     string   `yaml:"type,omitempty" json:"type,omitempty"`
	Username string   `yaml:"username,omitempty" json:"username,omitempty"`
	Password string   `yaml:"password,omitempty" json:"password,omitempty"`
	Host     string   `yaml:"host,omitempty" json:"host,omitempty"`
	Port     *uint64  `yaml:"port,omitempty" json:"port,omitempty"`
	Database string   `yaml:"database,omitempty" json:"database,omitempty"`
	SSLMode  string   `yaml:"sslmode,omitempty" json:"sslmode,omitempty"`
	Extras   []string `yaml:"extras,omitempty" json:"extras,omitempty"`

	*LocatableImpl `yaml:"-"`
}

ConnectionBlock for db connection Example:

connection:
   type: postgres
   username: postgres
   password: pass
   host: localhost
   port: 5432
   database: postgres
   sslmode: disable

func NewConnectionBlock

func NewConnectionBlock() *ConnectionBlock

func ParseConnectionBlockFromDSN

func ParseConnectionBlockFromDSN(dsn string) *ConnectionBlock

ParseConnectionBlockFromDSN convert dsn to connection block

func (*ConnectionBlock) BuildDSN

func (x *ConnectionBlock) BuildDSN() string

func (*ConnectionBlock) Check

func (x *ConnectionBlock) Check(module *Module, validatorContext *ValidatorContext) *schema.Diagnostics

func (*ConnectionBlock) IsEmpty

func (x *ConnectionBlock) IsEmpty() bool

type HaveRuntime

type HaveRuntime[T any] interface {

	// Runtime Returns the runtime corresponding to the block
	Runtime() T
}

HaveRuntime Some blocks may have a runtime to handle more complex logic

type Locatable

type Locatable interface {

	// GetNodeLocation Gets the location of the block
	GetNodeLocation(selector string) *NodeLocation

	// SetNodeLocation Set the location of the node
	SetNodeLocation(selector string, nodeLocation *NodeLocation) error
}

Locatable Used to find the file and location of each block. All blocks should implement this interface

type LocatableImpl

type LocatableImpl struct {
	// contains filtered or unexported fields
}

func NewLocatableImpl

func NewLocatableImpl() *LocatableImpl

func (*LocatableImpl) GetNodeLocation

func (x *LocatableImpl) GetNodeLocation(relativeSelector string) *NodeLocation

func (*LocatableImpl) SetNodeLocation

func (x *LocatableImpl) SetNodeLocation(relativeSelector string, nodeLocation *NodeLocation) error

type MergableBlock

type MergableBlock[T Block] interface {

	// Merge Used to merge two identical blocks
	Merge(other T) (T, *schema.Diagnostics)
}

MergableBlock Used to indicate that a block is merge

type Module

type Module struct {

	// Several root-level blocks of a module
	SelefraBlock   *SelefraBlock
	ModulesBlock   ModulesBlock
	ProvidersBlock ProvidersBlock
	RulesBlock     RulesBlock
	VariablesBlock VariablesBlock

	// Parent of the current module
	ParentModule *Module

	// What are the submodules of the current module, [subModuleName, *subModule]
	// Keep the order of references
	SubModules []*Module

	// The source of the module, in fact, is the string written inside use
	// The source of the root module is the current path
	Source string
	// Local path of the module
	ModuleLocalDirectory string

	// How is the dependency from the top-level module to the current module, in fact, all the way to use the concatenation
	DependenciesPath []string
}

Module Represents information about a module

func NewModule

func NewModule() *Module

func (*Module) BuildFullName

func (x *Module) BuildFullName() string

BuildFullName The full path name of the module, which can be understood at a glance

func (*Module) Check

func (x *Module) Check(module *Module, validatorContext *ValidatorContext) *schema.Diagnostics

func (*Module) HasRequiredProviderName

func (x *Module) HasRequiredProviderName(requiredProviderName string) bool

HasRequiredProviderName check whether the required provider name is available

func (*Module) ListRequiredProvidersName

func (x *Module) ListRequiredProvidersName() []string

ListRequiredProvidersName List the names of all required providers

func (*Module) Merge

func (x *Module) Merge(other *Module) (*Module, *schema.Diagnostics)

Merge the two modules into a new module

func (*Module) Traversal

func (x *Module) Traversal(ctx context.Context, traversalFunc func(ctx context.Context, traversalContext *TraversalContext) bool)

type ModuleBlock

type ModuleBlock struct {

	// Module name
	Name string `yaml:"name" json:"name"`

	// What other modules are referenced by this module
	Uses []string `yaml:"uses" json:"uses"`

	// The module supports specifying some variables
	Input map[string]any `yaml:"input" json:"input"`

	*LocatableImpl `yaml:"-"`
}

ModuleBlock Used to represent a common element in the modules array

func NewModuleBlock

func NewModuleBlock() *ModuleBlock

func (*ModuleBlock) Check

func (x *ModuleBlock) Check(module *Module, validatorContext *ValidatorContext) *schema.Diagnostics

func (*ModuleBlock) IsEmpty

func (x *ModuleBlock) IsEmpty() bool

type ModulesBlock

type ModulesBlock []*ModuleBlock

func (ModulesBlock) Check

func (x ModulesBlock) Check(module *Module, validatorContext *ValidatorContext) *schema.Diagnostics

func (ModulesBlock) GetNodeLocation

func (x ModulesBlock) GetNodeLocation(selector string) *NodeLocation

func (ModulesBlock) IsEmpty

func (x ModulesBlock) IsEmpty() bool

func (ModulesBlock) Merge

func (ModulesBlock) ModulesInputMap

func (x ModulesBlock) ModulesInputMap() map[string]*ModuleBlock

func (ModulesBlock) SetNodeLocation

func (x ModulesBlock) SetNodeLocation(selector string, nodeLocation *NodeLocation) error

type NodeLocation

type NodeLocation struct {

	// The path from the root node of yaml to the current node
	YamlSelector string

	// path for find file, It is usually stored in a file system, which is the location of a file
	Path string

	// Represents a continuous piece of text in a file, with a starting position and an ending position
	Begin, End *Position
}

NodeLocation A piece of location information used to represent a block

func BuildLocationFromYamlNode

func BuildLocationFromYamlNode(yamlFilePath string, yamlSelector string, node *yaml.Node) *NodeLocation

func MergeKeyValueLocation

func MergeKeyValueLocation(keyLocation, valueLocation *NodeLocation) *NodeLocation

func (*NodeLocation) ReadSourceString

func (x *NodeLocation) ReadSourceString() string

ReadSourceString Read the source string content based on location information

type Position

type Position struct {

	// which line
	Line int

	// which column
	Column int
}

Position Represents a point in a file

func NewPosition

func NewPosition(line, column int) *Position

type ProviderBlock

type ProviderBlock struct {

	// Name of the current block
	Name string

	// How long is the cache
	Cache string

	// Which of the selefra.providers is associated with
	Provider string

	// What is the maximum concurrency when pulling data
	MaxGoroutines *uint64

	// What resources need to be pulled? If you do not write, the default is to pull all resources
	Resources []string

	// What are the self-defined configurations of the provider? These should be passed to the provider through
	ProvidersConfigYamlString string

	*LocatableImpl `yaml:"-"`
}

ProviderBlock An element in the providers block array at the root level

func NewProviderBlock

func NewProviderBlock() *ProviderBlock

func (*ProviderBlock) Check

func (x *ProviderBlock) Check(module *Module, validatorContext *ValidatorContext) *schema.Diagnostics

func (*ProviderBlock) IsEmpty

func (x *ProviderBlock) IsEmpty() bool

func (*ProviderBlock) MarshalYAML

func (x *ProviderBlock) MarshalYAML() (interface{}, error)

type ProvidersBlock

type ProvidersBlock []*ProviderBlock

ProvidersBlock The root level providers block

func (ProvidersBlock) Check

func (x ProvidersBlock) Check(module *Module, validatorContext *ValidatorContext) *schema.Diagnostics

func (ProvidersBlock) GetNodeLocation

func (x ProvidersBlock) GetNodeLocation(selector string) *NodeLocation

func (ProvidersBlock) IsEmpty

func (x ProvidersBlock) IsEmpty() bool

func (ProvidersBlock) Merge

func (ProvidersBlock) SetNodeLocation

func (x ProvidersBlock) SetNodeLocation(selector string, nodeLocation *NodeLocation) error

func (ProvidersBlock) ToProviderNameMap

func (x ProvidersBlock) ToProviderNameMap() map[string]*ProviderBlock

type RequireProviderBlock

type RequireProviderBlock struct {

	// The name of this constraint
	Name string `yaml:"name,omitempty" json:"name,omitempty"`

	// Where does the Provider load from
	Source string `yaml:"source,omitempty" json:"source,omitempty"`

	// Version requirements for this provider
	Version string `yaml:"version,omitempty" json:"version,omitempty"`

	// The debug parameter, if configured, uses the given path instead of downloading
	Path string `yaml:"path,omitempty" json:"path,omitempty"`

	//runtime *RequireProviderBlockRuntime
	*LocatableImpl `yaml:"-"`
}

RequireProviderBlock Specifies the version of the Provider to be installed

func NewRequireProviderBlock

func NewRequireProviderBlock() *RequireProviderBlock

func (*RequireProviderBlock) Check

func (x *RequireProviderBlock) Check(module *Module, validatorContext *ValidatorContext) *schema.Diagnostics

func (*RequireProviderBlock) IsEmpty

func (x *RequireProviderBlock) IsEmpty() bool

type RequireProvidersBlock

type RequireProvidersBlock []*RequireProviderBlock

func (RequireProvidersBlock) BuildNameToProviderBlockMap

func (x RequireProvidersBlock) BuildNameToProviderBlockMap() map[string]*RequireProviderBlock

func (RequireProvidersBlock) Check

func (x RequireProvidersBlock) Check(module *Module, validatorContext *ValidatorContext) *schema.Diagnostics

func (RequireProvidersBlock) GetNodeLocation

func (x RequireProvidersBlock) GetNodeLocation(selector string) *NodeLocation

func (RequireProvidersBlock) IsEmpty

func (x RequireProvidersBlock) IsEmpty() bool

func (RequireProvidersBlock) Merge

func (RequireProvidersBlock) SetNodeLocation

func (x RequireProvidersBlock) SetNodeLocation(selector string, nodeLocation *NodeLocation) error

type RuleBlock

type RuleBlock struct {

	// Name of policy
	Name string `yaml:"name" json:"name"`

	// Query statement corresponding to the policy
	Query string `yaml:"query" json:"query"`

	// Some custom tags
	Labels map[string]string `yaml:"labels" json:"labels"`

	// Metadata for the policy
	MetadataBlock *RuleMetadataBlock `json:"metadata" yaml:"metadata"`

	// Policy output
	Output string `yaml:"output" json:"output"`

	*LocatableImpl `yaml:"-"`
}

RuleBlock Represents a rule block

func NewRuleBlock

func NewRuleBlock() *RuleBlock

func (*RuleBlock) Check

func (x *RuleBlock) Check(module *Module, validatorContext *ValidatorContext) *schema.Diagnostics

func (*RuleBlock) Copy

func (x *RuleBlock) Copy() *RuleBlock

func (*RuleBlock) IsEmpty

func (x *RuleBlock) IsEmpty() bool

type RuleMetadataBlock

type RuleMetadataBlock struct {

	// A globally unique policy ID
	Id string `yaml:"id" json:"id"`

	// The severity of the problem
	Severity string `yaml:"severity" json:"severity"`

	// The Provider to which it is associated
	Provider string `yaml:"provider" json:"provider"`

	// Some custom tags
	Tags []string `yaml:"tags" json:"tags"`

	// Who is the author of the strategy
	Author string `yaml:"author" json:"author"`

	// The fix must be a local file relative path that points to a Markdown file
	Remediation string `yaml:"remediation" json:"remediation"`

	// Bug title
	Title string `yaml:"title" json:"title"`

	// Some description of the Bug
	Description string `yaml:"description" json:"description"`

	*LocatableImpl `yaml:"-"`
	// contains filtered or unexported fields
}

RuleMetadataBlock Represents metadata information for a block

func NewRuleMetadataBlock

func NewRuleMetadataBlock(rule *RuleBlock) *RuleMetadataBlock

func (*RuleMetadataBlock) Check

func (x *RuleMetadataBlock) Check(module *Module, validatorContext *ValidatorContext) *schema.Diagnostics

func (*RuleMetadataBlock) Copy

func (*RuleMetadataBlock) IsEmpty

func (x *RuleMetadataBlock) IsEmpty() bool

type RuleMetadataBlockRuntime

type RuleMetadataBlockRuntime struct {
	// contains filtered or unexported fields
}

func NewRuleMetadataBlockRuntime

func NewRuleMetadataBlockRuntime(rule *RuleBlock) *RuleMetadataBlockRuntime

type RulesBlock

type RulesBlock []*RuleBlock

func (RulesBlock) Check

func (x RulesBlock) Check(module *Module, validatorContext *ValidatorContext) *schema.Diagnostics

func (RulesBlock) GetNodeLocation

func (x RulesBlock) GetNodeLocation(selector string) *NodeLocation

func (RulesBlock) IsEmpty

func (x RulesBlock) IsEmpty() bool

func (RulesBlock) Merge

func (x RulesBlock) Merge(other RulesBlock) (RulesBlock, *schema.Diagnostics)

func (RulesBlock) SetNodeLocation

func (x RulesBlock) SetNodeLocation(selector string, nodeLocation *NodeLocation) error

type SelefraBlock

type SelefraBlock struct {

	// Name of project
	Name string `yaml:"name,omitempty" mapstructure:"name,omitempty"`

	// selefra CloudBlock-related configuration
	CloudBlock *CloudBlock `yaml:"cloud,omitempty" mapstructure:"cloud,omitempty"`

	// The version of the cli used by the project
	CliVersion string `yaml:"cli_version,omitempty" mapstructure:"cli_version,omitempty"`

	// Global log level. This level is used when the provider does not specify a log level
	LogLevel string `yaml:"log_level,omitempty" mapstructure:"log_level,omitempty"`

	//What are the providers required for operation
	RequireProvidersBlock RequireProvidersBlock `yaml:"providers,omitempty" mapstructure:"providers,omitempty"`

	// The configuration required to connect to the database
	ConnectionBlock *ConnectionBlock `yaml:"connection,omitempty" mapstructure:"connection,omitempty"`

	*LocatableImpl `yaml:"-"`
}

SelefraBlock One of the root-level blocks

func NewSelefraBlock

func NewSelefraBlock() *SelefraBlock

func (*SelefraBlock) Check

func (x *SelefraBlock) Check(module *Module, validatorContext *ValidatorContext) *schema.Diagnostics

func (*SelefraBlock) IsEmpty

func (x *SelefraBlock) IsEmpty() bool

func (*SelefraBlock) Merge

type TraversalContext

type TraversalContext struct {
	ParentTraversalContext *TraversalContext

	ParentModule *Module
	Module       *Module
}

type Validator

type Validator interface {

	// Check whether the node configuration is correct
	Check(module *Module, validatorContext *ValidatorContext) *schema.Diagnostics
}

Validator A validator that supports checking

type ValidatorContext

type ValidatorContext struct {

	// Global collection of rule ids
	RulesIdSet map[string]*RuleBlock

	// All module names, if there are module names such as the same name should be able to check out
	ModuleNameSet map[string]*ModuleBlock
}

ValidatorContext Some global context information stored during validation

func NewValidatorContext

func NewValidatorContext() *ValidatorContext

NewValidatorContext Create a validation context

func (*ValidatorContext) AddModuleBlock

func (x *ValidatorContext) AddModuleBlock(moduleBlock *ModuleBlock)

AddModuleBlock Adds the module to the current validator context

func (*ValidatorContext) AddRuleBlock

func (x *ValidatorContext) AddRuleBlock(ruleBlock *RuleBlock)

AddRuleBlock Add rules to the validation context

func (*ValidatorContext) GetModuleByName

func (x *ValidatorContext) GetModuleByName(moduleName string) (*ModuleBlock, bool)

GetModuleByName Gets the module in the validation context

func (*ValidatorContext) GetRuleBlockById

func (x *ValidatorContext) GetRuleBlockById(ruleId string) (*RuleBlock, bool)

GetRuleBlockById Determine whether the given rule is in context

type VariableBlock

type VariableBlock struct {

	// Name of a variable
	Key string `yaml:"key" json:"key"`

	// The default value of the variable
	Default any `yaml:"default" json:"default"`

	// A description of this variable
	Description string `yaml:"description" json:"description"`

	// Who is the author of the variable? What the hell is this?
	Author string `yaml:"author" json:"author"`

	*LocatableImpl `yaml:"-"`
}

VariableBlock Used to declare a variable

func NewVariableBlock

func NewVariableBlock() *VariableBlock

func (*VariableBlock) Check

func (x *VariableBlock) Check(module *Module, validatorContext *ValidatorContext) *schema.Diagnostics

func (*VariableBlock) IsEmpty

func (x *VariableBlock) IsEmpty() bool

type VariablesBlock

type VariablesBlock []*VariableBlock

VariablesBlock One of the root-level code blocks

func (VariablesBlock) Check

func (x VariablesBlock) Check(module *Module, validatorContext *ValidatorContext) *schema.Diagnostics

func (VariablesBlock) GetNodeLocation

func (x VariablesBlock) GetNodeLocation(selector string) *NodeLocation

func (VariablesBlock) IsEmpty

func (x VariablesBlock) IsEmpty() bool

func (VariablesBlock) Merge

func (VariablesBlock) SetNodeLocation

func (x VariablesBlock) SetNodeLocation(selector string, nodeLocation *NodeLocation) error

Jump to

Keyboard shortcuts

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