From 9853b6799909414838d66f9f6ec8e05dc1c79eb1 Mon Sep 17 00:00:00 2001 From: Roshick Date: Wed, 2 Nov 2022 13:14:26 +0100 Subject: [PATCH] feat(#37): Make additional promoters configurable on a user level --- README.md | 1 + acorns/repository/customconfigint.go | 1 + internal/repository/config/accessors.go | 6 ++- internal/repository/config/config.go | 44 +++++++++++-------- internal/repository/config/plumbing.go | 38 ++++++++-------- internal/repository/config/validation_test.go | 1 + internal/service/services/services.go | 15 ++++++- internal/service/services/services_test.go | 5 +++ .../service-promoters-with-additionals.json | 1 + .../service-promoters.json | 1 + test/resources/valid-config-unique.yaml | 2 + test/resources/valid-config.yaml | 2 + 12 files changed, 79 insertions(+), 38 deletions(-) diff --git a/README.md b/README.md index 4479948..92711a7 100644 --- a/README.md +++ b/README.md @@ -64,6 +64,7 @@ the [`local-config.yaml`][config] can be used to set the variables. | `ALERT_TARGET_PREFIX` | | Validates the alert target to either match the prefix or suffix. | | `ALERT_TARGET_SUFFIX` | | | | | | | +| `ADDITIONAL_PROMOTERS` | | promoters to be added for all services. Can be left empty, or contain a comma separated list of usernames | | `ADDITIONAL_PROMOTERS_FROM_OWNERS` | | owner aliases from which to get additional promoters to be added for all services. Can be left empty, or contain a comma separated list of owner aliases | ## Datastore diff --git a/acorns/repository/customconfigint.go b/acorns/repository/customconfigint.go index 9548497..dec0d60 100644 --- a/acorns/repository/customconfigint.go +++ b/acorns/repository/customconfigint.go @@ -28,6 +28,7 @@ type CustomConfiguration interface { AlertTargetPrefix() string AlertTargetSuffix() string + AdditionalPromoters() []string AdditionalPromotersFromOwners() []string ElasticApmEnabled() bool diff --git a/internal/repository/config/accessors.go b/internal/repository/config/accessors.go index a95b088..0349a10 100644 --- a/internal/repository/config/accessors.go +++ b/internal/repository/config/accessors.go @@ -69,10 +69,14 @@ func (c *CustomConfigImpl) AlertTargetSuffix() string { return c.VAlertTargetSuffix } -func (c *CustomConfigImpl) AdditionalPromotersFromOwners() []string { +func (c *CustomConfigImpl) AdditionalPromoters() []string { return strings.Split(c.VAdditionalPromoters, ",") } +func (c *CustomConfigImpl) AdditionalPromotersFromOwners() []string { + return strings.Split(c.VAdditionalPromotersFromOwners, ",") +} + func (c *CustomConfigImpl) ElasticApmEnabled() bool { return !c.VElasticApmDisabled && os.Getenv("ELASTIC_APM_SERVER_URL") != "" && diff --git a/internal/repository/config/config.go b/internal/repository/config/config.go index fbbcf38..162079e 100644 --- a/internal/repository/config/config.go +++ b/internal/repository/config/config.go @@ -7,24 +7,25 @@ import ( ) const ( - KeyBbUser = "BB_USER" - KeyGitCommitterName = "GIT_COMMITTER_NAME" - KeyGitCommitterEmail = "GIT_COMMITTER_EMAIL" - KeyKafkaUser = "KAFKA_USER" - KeyKafkaTopic = "KAFKA_TOPIC" - KeyKafkaSeedBrokers = "KAFKA_SEED_BROKERS" - KeyKafkaGroupIdOverride = "KAFKA_GROUP_ID_OVERRIDE" - KeyKeySetUrl = "KEY_SET_URL" - KeyMetadataRepoUrl = "METADATA_REPO_URL" - KeyOwnerRegex = "OWNER_REGEX" - KeyUpdateJobIntervalMinutes = "UPDATE_JOB_INTERVAL_MINUTES" - KeyUpdateJobTimeoutSeconds = "UPDATE_JOB_TIMEOUT_SECONDS" - KeyVaultSecretsBasePath = "VAULT_SECRETS_BASE_PATH" - KeyVaultKafkaSecretPath = "VAULT_KAFKA_SECRET_PATH" - KeyAlertTargetPrefix = "ALERT_TARGET_PREFIX" - KeyAlertTargetSuffix = "ALERT_TARGET_SUFFIX" - KeyAdditionalPromoters = "ADDITIONAL_PROMOTERS_FROM_OWNERS" - KeyElasticApmDisabled = "ELASTIC_APM_DISABLED" + KeyBbUser = "BB_USER" + KeyGitCommitterName = "GIT_COMMITTER_NAME" + KeyGitCommitterEmail = "GIT_COMMITTER_EMAIL" + KeyKafkaUser = "KAFKA_USER" + KeyKafkaTopic = "KAFKA_TOPIC" + KeyKafkaSeedBrokers = "KAFKA_SEED_BROKERS" + KeyKafkaGroupIdOverride = "KAFKA_GROUP_ID_OVERRIDE" + KeyKeySetUrl = "KEY_SET_URL" + KeyMetadataRepoUrl = "METADATA_REPO_URL" + KeyOwnerRegex = "OWNER_REGEX" + KeyUpdateJobIntervalMinutes = "UPDATE_JOB_INTERVAL_MINUTES" + KeyUpdateJobTimeoutSeconds = "UPDATE_JOB_TIMEOUT_SECONDS" + KeyVaultSecretsBasePath = "VAULT_SECRETS_BASE_PATH" + KeyVaultKafkaSecretPath = "VAULT_KAFKA_SECRET_PATH" + KeyAlertTargetPrefix = "ALERT_TARGET_PREFIX" + KeyAlertTargetSuffix = "ALERT_TARGET_SUFFIX" + KeyAdditionalPromoters = "ADDITIONAL_PROMOTERS" + KeyAdditionalPromotersFromOwners = "ADDITIONAL_PROMOTERS_FROM_OWNERS" + KeyElasticApmDisabled = "ELASTIC_APM_DISABLED" ) var CustomConfigItems = []auconfigapi.ConfigItem{ @@ -142,6 +143,13 @@ var CustomConfigItems = []auconfigapi.ConfigItem{ Key: KeyAdditionalPromoters, EnvName: KeyAdditionalPromoters, Default: "", + Description: "promoters to be added for all services. Can be left empty, or contain a comma separated list of usernames", + Validate: auconfigenv.ObtainPatternValidator("^|[a-z](-?[a-z0-9]+)*(,[a-z](-?[a-z0-9]+)*)*$"), + }, + { + Key: KeyAdditionalPromotersFromOwners, + EnvName: KeyAdditionalPromotersFromOwners, + Default: "", Description: "owner aliases from which to get additional promoters to be added for all services. Can be left empty, or contain a comma separated list of owner aliases", Validate: auconfigenv.ObtainPatternValidator("^|[a-z](-?[a-z0-9]+)*(,[a-z](-?[a-z0-9]+)*)*$"), }, diff --git a/internal/repository/config/plumbing.go b/internal/repository/config/plumbing.go index 3470701..3ce265f 100644 --- a/internal/repository/config/plumbing.go +++ b/internal/repository/config/plumbing.go @@ -8,24 +8,25 @@ import ( ) type CustomConfigImpl struct { - VBbUser string - VGitCommitterName string - VGitCommitterEmail string - VKafkaUser string - VKafkaTopic string - VKafkaSeedBrokers string - VKeySetUrl string - VKafkaGroupIdOverride string - VMetadataRepoUrl string - VOwnerRegex string - VUpdateJobIntervalCronPart string - VUpdateJobTimeoutSeconds uint16 - VVaultSecretsBasePath string - VVaultKafkaSecretPath string - VAlertTargetPrefix string - VAlertTargetSuffix string - VAdditionalPromoters string - VElasticApmDisabled bool + VBbUser string + VGitCommitterName string + VGitCommitterEmail string + VKafkaUser string + VKafkaTopic string + VKafkaSeedBrokers string + VKeySetUrl string + VKafkaGroupIdOverride string + VMetadataRepoUrl string + VOwnerRegex string + VUpdateJobIntervalCronPart string + VUpdateJobTimeoutSeconds uint16 + VVaultSecretsBasePath string + VVaultKafkaSecretPath string + VAlertTargetPrefix string + VAlertTargetSuffix string + VAdditionalPromoters string + VAdditionalPromotersFromOwners string + VElasticApmDisabled bool } func New() auacornapi.Acorn { @@ -51,6 +52,7 @@ func (c *CustomConfigImpl) Obtain(getter func(key string) string) { c.VAlertTargetPrefix = getter(KeyAlertTargetPrefix) c.VAlertTargetSuffix = getter(KeyAlertTargetSuffix) c.VAdditionalPromoters = getter(KeyAdditionalPromoters) + c.VAdditionalPromotersFromOwners = getter(KeyAdditionalPromotersFromOwners) c.VElasticApmDisabled, _ = strconv.ParseBool(getter(KeyElasticApmDisabled)) } diff --git a/internal/repository/config/validation_test.go b/internal/repository/config/validation_test.go index d4b7e38..db7de17 100644 --- a/internal/repository/config/validation_test.go +++ b/internal/repository/config/validation_test.go @@ -135,5 +135,6 @@ func TestAccessors(t *testing.T) { require.Equal(t, "kafka/feat/room-service", repository.Custom(cut).VaultKafkaSecretPath()) require.Equal(t, "https://some-domain.com/", repository.Custom(cut).AlertTargetPrefix()) require.Equal(t, "@some-domain.com", repository.Custom(cut).AlertTargetSuffix()) + require.EqualValues(t, []string{"someguy"}, repository.Custom(cut).AdditionalPromoters()) require.EqualValues(t, []string{"add-my-promoters-to-every-service", "also-add-my-promoters"}, repository.Custom(cut).AdditionalPromotersFromOwners()) } diff --git a/internal/service/services/services.go b/internal/service/services/services.go index a85dfcd..acb1719 100644 --- a/internal/service/services/services.go +++ b/internal/service/services/services.go @@ -387,8 +387,14 @@ func (s *Impl) validateDeletionDto(ctx context.Context, deletionInfo openapi.Del func (s *Impl) GetServicePromoters(ctx context.Context, serviceOwnerAlias string) (openapi.ServicePromotersDto, error) { resultSet := make(map[string]bool) + // add default promoters + err := s.addDefaultPromoters(ctx, resultSet) + if err != nil { + return openapi.ServicePromotersDto{}, err + } + // add the promoters for the given ownerAlias - err := s.addPromotersForOwner(ctx, serviceOwnerAlias, resultSet) + err = s.addPromotersForOwner(ctx, serviceOwnerAlias, resultSet) if err != nil { return openapi.ServicePromotersDto{}, err } @@ -417,6 +423,13 @@ func (s *Impl) GetServicePromoters(ctx context.Context, serviceOwnerAlias string return openapi.ServicePromotersDto{Promoters: result}, nil } +func (s *Impl) addDefaultPromoters(ctx context.Context, resultSet map[string]bool) error { + for _, user := range s.CustomConfiguration.AdditionalPromoters() { + resultSet[user] = true + } + return nil +} + func (s *Impl) addPromotersForOwner(ctx context.Context, ownerAlias string, resultSet map[string]bool) error { serviceOwner, err := s.Cache.GetOwner(ctx, ownerAlias) if err != nil { diff --git a/internal/service/services/services_test.go b/internal/service/services/services_test.go index 940ba60..3656901 100644 --- a/internal/service/services/services_test.go +++ b/internal/service/services/services_test.go @@ -131,6 +131,11 @@ func tstCreateValid() openapi.ServiceCreateDto { type MockConfig struct { } +func (c *MockConfig) AdditionalPromoters() []string { + //TODO implement me + panic("implement me") +} + func (c *MockConfig) ElasticApmEnabled() bool { return false } diff --git a/test/resources/acceptance-expected/service-promoters-with-additionals.json b/test/resources/acceptance-expected/service-promoters-with-additionals.json index 75fae0f..0b9a78b 100644 --- a/test/resources/acceptance-expected/service-promoters-with-additionals.json +++ b/test/resources/acceptance-expected/service-promoters-with-additionals.json @@ -3,6 +3,7 @@ "else", "entirely", "kschlangenheldt", + "someguy", "someone" ] } \ No newline at end of file diff --git a/test/resources/acceptance-expected/service-promoters.json b/test/resources/acceptance-expected/service-promoters.json index fabcbb9..6bf725d 100644 --- a/test/resources/acceptance-expected/service-promoters.json +++ b/test/resources/acceptance-expected/service-promoters.json @@ -2,6 +2,7 @@ "promoters": [ "else", "kschlangenheldt", + "someguy", "someone" ] } \ No newline at end of file diff --git a/test/resources/valid-config-unique.yaml b/test/resources/valid-config-unique.yaml index db291af..ace2cdc 100644 --- a/test/resources/valid-config-unique.yaml +++ b/test/resources/valid-config-unique.yaml @@ -27,4 +27,6 @@ OWNER_REGEX: '[a-z][0-9]+' VAULT_KAFKA_SECRET_PATH: kafka/feat/room-service ALERT_TARGET_PREFIX: https://some-domain.com/ ALERT_TARGET_SUFFIX: '@some-domain.com' + +ADDITIONAL_PROMOTERS: 'someguy' ADDITIONAL_PROMOTERS_FROM_OWNERS: 'add-my-promoters-to-every-service,also-add-my-promoters' \ No newline at end of file diff --git a/test/resources/valid-config.yaml b/test/resources/valid-config.yaml index 80da023..46f55d5 100644 --- a/test/resources/valid-config.yaml +++ b/test/resources/valid-config.yaml @@ -13,4 +13,6 @@ BB_USER: localuser METADATA_REPO_URL: http://metadata OWNER_REGEX: .* UPDATE_JOB_INTERVAL_MINUTES: 5 + +ADDITIONAL_PROMOTERS: 'someguy' ADDITIONAL_PROMOTERS_FROM_OWNERS: 'deleteme' \ No newline at end of file