Skip to content

Commit

Permalink
review feedback: stop reading an environmental variable inside a func…
Browse files Browse the repository at this point in the history
…tion, and instead use the viper library for that
  • Loading branch information
Alexander Kjäll committed Dec 18, 2023
1 parent 29bd987 commit 666953a
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 45 deletions.
37 changes: 21 additions & 16 deletions cmd/vulcan-api/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,22 +146,27 @@ type awsCatalogueConfig struct {
RetryInterval int `mapstructure:"retry_interval"`
}

type dnsHostnameValidation struct {
DnsHostnameValidation string `mapstructure:"dns_hostname_validation"`
}

type config struct {
Server serverConfig
DB dbConfig
Log logConfig
SAML samlConfig
Defaults store.DefaultEntities
ScanEngine scanengine.Config
Scheduler schedule.Config
Reports reports.Config
VulcanCore vulcanCoreConfig
VulnerabilityDB vulnerabilityDBConfig
VulcanTracker vulcantrackerConfig
Metrics metricsConfig
AWSCatalogue awsCatalogueConfig
Kafka kafkaConfig `mapstructure:"kafka"`
GlobalPolicyConfig global.GlobalPolicyConfig `mapstructure:"globalpolicy"`
Server serverConfig
DB dbConfig
Log logConfig
SAML samlConfig
Defaults store.DefaultEntities
ScanEngine scanengine.Config
Scheduler schedule.Config
Reports reports.Config
VulcanCore vulcanCoreConfig
VulnerabilityDB vulnerabilityDBConfig
VulcanTracker vulcantrackerConfig
Metrics metricsConfig
AWSCatalogue awsCatalogueConfig
Kafka kafkaConfig `mapstructure:"kafka"`
GlobalPolicyConfig global.GlobalPolicyConfig `mapstructure:"globalpolicy"`
DnsHostnameValidation dnsHostnameValidation
}

func initConfig() {
Expand Down Expand Up @@ -277,7 +282,7 @@ func startServer() error {
// Add global middleware to the vulcanito service.
vulcanitoService = globalMiddleware(vulcanitoService)

endpoints := endpoint.MakeEndpoints(vulcanitoService, vulcantrackerClient != nil, logger)
endpoints := endpoint.MakeEndpoints(vulcanitoService, vulcantrackerClient != nil, logger, strings.EqualFold(cfg.DnsHostnameValidation.DnsHostnameValidation, "true"))

endpoints = addAuthorizationMiddleware(endpoints, db, logger)
endpoints = addWhitelistingMiddleware(endpoints, logger)
Expand Down
3 changes: 3 additions & 0 deletions config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,6 @@ topics = $KAFKA_TOPICS
# Leave this entry at the end so run.sh can fill dynamically
# global program policy configurations accordingly.
[globalpolicy]

[assets]
dns_hostname_validaton = "$DNS_HOSTNAME_VALIDATION"
10 changes: 4 additions & 6 deletions pkg/api/asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func validateAWSARN(arn string) bool {
}

// Validate checks if an asset is valid.
func (a Asset) Validate() error {
func (a Asset) Validate(dnsHostnameValidation bool) error {
err := validator.New().Struct(a)
if err != nil {
return errors.Validation(err)
Expand All @@ -68,14 +68,12 @@ func (a Asset) Validate() error {

switch a.AssetType.Name {
case "Hostname":
if os.Getenv("VULCAN_HOSTNAME_VALIDATION_WITH_DNS") == "false" {
if !types.IsHostnameNoDnsResolution(a.Identifier) {
return errors.Validation("Identifier is not a valid Hostname")
}
} else {
if dnsHostnameValidation {
if !types.IsHostname(a.Identifier) {
return errors.Validation("Identifier is not a valid Hostname")
}
} else if !types.IsHostnameNoDnsResolution(a.Identifier) {
return errors.Validation("Identifier is not a valid Hostname")
}
case "AWSAccount":
if !validateAWSARN(a.Identifier) || !types.IsAWSARN(a.Identifier) {
Expand Down
8 changes: 4 additions & 4 deletions pkg/api/endpoint/assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func makeListAssetsEndpoint(s api.VulcanitoService, logger kitlog.Logger) endpoi
}

// makeCreateAssetEndpoint returns an endpoint that creates new assets.
func makeCreateAssetEndpoint(s api.VulcanitoService, logger kitlog.Logger) endpoint.Endpoint {
func makeCreateAssetEndpoint(s api.VulcanitoService, logger kitlog.Logger, dnsHostnameValidation bool) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
// We are expecting an assets list.
requestBody, ok := request.(*AssetsListRequest)
Expand Down Expand Up @@ -115,7 +115,7 @@ func makeCreateAssetEndpoint(s api.VulcanitoService, logger kitlog.Logger) endpo
annotations := requestBody.Annotations.ToModel()

// Ask for the service layer to create the assets.
createdAssets, err := s.CreateAssets(ctx, assets, groups, annotations)
createdAssets, err := s.CreateAssets(ctx, assets, groups, annotations, dnsHostnameValidation)
if err != nil {
return nil, err
}
Expand All @@ -132,7 +132,7 @@ func makeCreateAssetEndpoint(s api.VulcanitoService, logger kitlog.Logger) endpo
}

// makeCreateAssetMultiStatusEndpoint returns an endpoint that creates new assets.
func makeCreateAssetMultiStatusEndpoint(s api.VulcanitoService, logger kitlog.Logger) endpoint.Endpoint {
func makeCreateAssetMultiStatusEndpoint(s api.VulcanitoService, logger kitlog.Logger, dnsHostnameValidation bool) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
// We are expecting an assets list.
requestBody, ok := request.(*AssetsListRequest)
Expand Down Expand Up @@ -162,7 +162,7 @@ func makeCreateAssetMultiStatusEndpoint(s api.VulcanitoService, logger kitlog.Lo
annotations := requestBody.Annotations.ToModel()

// Ask for the service layer to create the assets.
responses, err := s.CreateAssetsMultiStatus(ctx, assets, groups, annotations)
responses, err := s.CreateAssetsMultiStatus(ctx, assets, groups, annotations, dnsHostnameValidation)
if err != nil {
return nil, err
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/api/endpoint/endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ type Endpoints map[string]endpoint.Endpoint
var endpoints = make(Endpoints)

// MakeEndpoints initialize endpoints using the given service
func MakeEndpoints(s api.VulcanitoService, isJiraIntEnabled bool, logger log.Logger) Endpoints {
func MakeEndpoints(s api.VulcanitoService, isJiraIntEnabled bool, logger log.Logger, dnsHostnameValidation bool) Endpoints {
endpoints[Healthcheck] = makeHealthcheckEndpoint(s, logger)

endpoints[FindJob] = makeFindJobEndpoint(s, logger)
Expand Down Expand Up @@ -157,8 +157,8 @@ func MakeEndpoints(s api.VulcanitoService, isJiraIntEnabled bool, logger log.Log
endpoints[UpdateRecipients] = makeUpdateRecipientsEndpoint(s, logger)

endpoints[ListAssets] = makeListAssetsEndpoint(s, logger)
endpoints[CreateAsset] = makeCreateAssetEndpoint(s, logger)
endpoints[CreateAssetMultiStatus] = makeCreateAssetMultiStatusEndpoint(s, logger)
endpoints[CreateAsset] = makeCreateAssetEndpoint(s, logger, dnsHostnameValidation)
endpoints[CreateAssetMultiStatus] = makeCreateAssetMultiStatusEndpoint(s, logger, dnsHostnameValidation)
endpoints[MergeDiscoveredAssets] = makeMergeDiscoveredAssetsEndpoint(s, logger)
endpoints[FindAsset] = makeFindAssetEndpoint(s, logger)
endpoints[UpdateAsset] = makeUpdateAssetEndpoint(s, logger)
Expand Down
16 changes: 8 additions & 8 deletions pkg/api/service/assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (s vulcanitoService) ListAssets(ctx context.Context, teamID string, asset a
}

// CreateAssets receives an array of assets and creates them on store layer.
func (s vulcanitoService) CreateAssets(ctx context.Context, assets []api.Asset, groups []api.Group, annotations []*api.AssetAnnotation) ([]api.Asset, error) {
func (s vulcanitoService) CreateAssets(ctx context.Context, assets []api.Asset, groups []api.Group, annotations []*api.AssetAnnotation, dnsHostnameValidation bool) ([]api.Asset, error) {
assetsToCreate := []api.Asset{}

// If no group is specified, add Default group data to groups list.
Expand Down Expand Up @@ -86,14 +86,14 @@ func (s vulcanitoService) CreateAssets(ctx context.Context, assets []api.Asset,
asset.AssetTypeID = assetTypeObj.ID
asset.AssetType = &api.AssetType{Name: assetTypeObj.Name}

if err := asset.Validate(); err != nil {
if err := asset.Validate(dnsHostnameValidation); err != nil {
return nil, err
}
assetsToCreate = append(assetsToCreate, asset)
} else {
// Asset type NOT provided by the user in the request. Try to infere
// asset type based on the identifier
assetsDetected, err := s.detectAssets(ctx, asset)
assetsDetected, err := s.detectAssets(ctx, asset, dnsHostnameValidation)
if err != nil {
return nil, errors.Validation(err, "asset", asset.Identifier, asset.AssetType.Name)
}
Expand Down Expand Up @@ -135,7 +135,7 @@ func (s vulcanitoService) getAccountName(identifier string) string {
// CreateAssetsMultiStatus receives an array of assets and request their creation to the store layer.
// Also, this method will associate the assets with the specified groups.
// It returns an array containing one response per request, in the same order as in the original request.
func (s vulcanitoService) CreateAssetsMultiStatus(ctx context.Context, assets []api.Asset, groups []api.Group, annotations []*api.AssetAnnotation) ([]api.AssetCreationResponse, error) {
func (s vulcanitoService) CreateAssetsMultiStatus(ctx context.Context, assets []api.Asset, groups []api.Group, annotations []*api.AssetAnnotation, dnsHostnameValidation bool) ([]api.AssetCreationResponse, error) {
responses := []api.AssetCreationResponse{}

// If no group is specified, add Default group data to groups list.
Expand Down Expand Up @@ -198,7 +198,7 @@ func (s vulcanitoService) CreateAssetsMultiStatus(ctx context.Context, assets []
asset.AssetType = &api.AssetType{Name: assetTypeObj.Name}

// If the asset is invalid, abort the asset creation.
if err := asset.Validate(); err != nil {
if err := asset.Validate(dnsHostnameValidation); err != nil {
response.Status = err
responses = append(responses, response)
continue
Expand All @@ -208,7 +208,7 @@ func (s vulcanitoService) CreateAssetsMultiStatus(ctx context.Context, assets []

} else {
// If user did not specify the asset type, auto detect it.
assetsDetected, err := s.detectAssets(ctx, asset)
assetsDetected, err := s.detectAssets(ctx, asset, dnsHostnameValidation)
if err != nil {
response.Status = errors.Validation(err, "asset", asset.Identifier, asset.AssetType.Name)
responses = append(responses, response)
Expand Down Expand Up @@ -527,7 +527,7 @@ func (s vulcanitoService) MergeDiscoveredAssetsAsync(ctx context.Context, teamID
return s.db.MergeAssetsAsync(teamID, assets, groupName)
}

func (s vulcanitoService) detectAssets(ctx context.Context, asset api.Asset) ([]api.Asset, error) {
func (s vulcanitoService) detectAssets(ctx context.Context, asset api.Asset, dnsHostnameValidation bool) ([]api.Asset, error) {
assets, err := getTypesFromIdentifier(asset.Identifier)
if err != nil {
return nil, err
Expand Down Expand Up @@ -565,7 +565,7 @@ func (s vulcanitoService) detectAssets(ctx context.Context, asset api.Asset) ([]
}

// Validate asset model before appending it to the results
if err = asset.Validate(); err != nil {
if err = asset.Validate(dnsHostnameValidation); err != nil {
return nil, err
}

Expand Down
12 changes: 6 additions & 6 deletions pkg/api/service/logging.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions pkg/api/vulcanito.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ type VulcanitoService interface {

// Assets
ListAssets(ctx context.Context, teamID string, asset Asset) ([]*Asset, error)
CreateAssets(ctx context.Context, assets []Asset, groups []Group, annotations []*AssetAnnotation) ([]Asset, error)
CreateAssetsMultiStatus(ctx context.Context, assets []Asset, groups []Group, annotations []*AssetAnnotation) ([]AssetCreationResponse, error)
CreateAssets(ctx context.Context, assets []Asset, groups []Group, annotations []*AssetAnnotation, dnsHostnameValidation bool) ([]Asset, error)
CreateAssetsMultiStatus(ctx context.Context, assets []Asset, groups []Group, annotations []*AssetAnnotation, dnsHostnameValidation bool) ([]AssetCreationResponse, error)
MergeDiscoveredAssets(ctx context.Context, teamID string, assets []Asset, groupName string) error
MergeDiscoveredAssetsAsync(ctx context.Context, teamID string, assets []Asset, groupName string) (*Job, error)
FindAsset(ctx context.Context, asset Asset) (*Asset, error)
Expand Down

0 comments on commit 666953a

Please sign in to comment.