init service now uses password resolution

This commit is contained in:
Michael Jerger 2023-05-18 14:00:51 +02:00
parent ca6b693a9a
commit 57a43085c0
5 changed files with 77 additions and 22 deletions

View file

@ -32,8 +32,8 @@ class DevopsFactory:
return devops
def merge(self, input, autorization, context) -> dict:
pass
def merge(self, input: dict, context: dict, authorization: dict) -> dict:
return {} | input | context | authorization
def __parse_build_types__(self, build_types: List[str]) -> List[BuildType]:
result = []

View file

@ -38,19 +38,20 @@ class InitService:
{
"gopass_path": "server/meissa/grafana-cloud",
"gopass_field": "grafana-cloud-user",
"name": "c4k_grafana_cloud_user",
},
{
"gopass_path": "server/meissa/grafana-cloud",
"name": "grafana_cloud_password",
"name": "c4k_grafana_cloud_password",
},
]
credentials = Credentials(input, default_mappings)
passwords = self.resolve_passwords(credentials)
# merge passwords & input
authorization = self.resolve_passwords(credentials)
merged = self.devops_factory.merge(input, {}, authorization)
if MixinType.RELEASE in mixin_types:
primary_build_file_id = input.get(
primary_build_file_id = merged.get(
"release_primary_build_file", "./project.clj"
)
primary_build_file = self.build_file_repository.get(
@ -58,21 +59,21 @@ class InitService:
)
version = primary_build_file.get_version()
return self.devops_factory.build_devops(input, version=version)
return self.devops_factory.build_devops(merged, version=version)
def resolve_passwords(self, credentials: Credentials) -> List[str]:
result = {}
for name in credentials.mappings.keys():
mapping = credentials.mappings[name]
env_value = self.environment_api.get(mapping.name_for_environment)
env_value = self.environment_api.get(mapping.name_for_environment())
if env_value:
result[name] = env_value
else:
if mapping.gopass_type == GopassType.FIELD:
if mapping.gopass_type() == GopassType.FIELD:
result[name] = self.credentials_api.gopass_field_from_path(
mapping.gopass_path, mapping.gopass_field
)
if mapping.gopass_type == GopassType.PASSWORD:
if mapping.gopass_type() == GopassType.PASSWORD:
result[name] = self.credentials_api.gopass_password_from_path(
mapping.gopass_path
)

View file

@ -24,6 +24,12 @@ def devops_config(overrides: dict) -> dict:
"release_current_branch": "my_feature",
"release_primary_build_file": "./package.json",
"release_secondary_build_file": [],
"credentials_mappings": [
{
"gopass_path": "a/path",
"gopass_field": "a-field",
},
],
}
input = default.copy()
input.update(overrides)
@ -50,15 +56,21 @@ class BuildFileRepositoryMock:
def write(self, build_file: BuildFile):
pass
class EnvironmentApiMock():
class EnvironmentApiMock:
def __init__(self, mappings):
self.mappings = mappings
def get(self, key):
pass
return self.mappings.get(key, None)
class CredentialsApiMock():
def gopass_field_from_path (self, path, field):
pass
class CredentialsApiMock:
def __init__(self, mappings):
self.mappings = mappings
def gopass_password_from_path (elf, path):
pass
def gopass_field_from_path(self, path, field):
return self.mappings.get(f"{path}:{field}", None)
def gopass_password_from_path(self, path):
return self.mappings.get(path, None)

View file

@ -30,6 +30,17 @@ def test_should_create_mapping():
assert "GRAFANA_CLOUD_PASSWORD" == sut.name_for_environment()
assert GopassType.PASSWORD == sut.gopass_type()
sut = CredentialMapping(
{
"gopass_path": "server/meissa/grafana-cloud",
"gopass_field": "grafana-cloud-user",
"name": "gfc_user",
}
)
assert "gfc_user" == sut.name_for_input()
assert "GFC_USER" == sut.name_for_environment()
assert GopassType.FIELD == sut.gopass_type()
def test_should_validate_CredentialMapping():
sut = CredentialMapping(

View file

@ -4,18 +4,49 @@ from src.main.python.ddadevops.domain import (
DevopsFactory,
Version,
MixinType,
BuildType,
)
from .helper import (
BuildFileRepositoryMock,
EnvironmentApiMock,
CredentialsApiMock,
devops_config,
)
from .helper import BuildFileRepositoryMock, EnvironmentApiMock, CredentialsApiMock, devops_config
def test_sould_load_build_file():
def test_should_load_build_file():
sut = InitService(
DevopsFactory(),
BuildFileRepositoryMock(),
CredentialsApiMock(),
EnvironmentApiMock(),
CredentialsApiMock({
"server/meissa/grafana-cloud:grafana-cloud-user": "gopass-gfc-user",
"server/meissa/grafana-cloud": "gopass-gfc-password",
}),
EnvironmentApiMock({}),
)
assert (
Version.from_str("1.1.5-SNAPSHOT")
== sut.initialize(devops_config({})).mixins[MixinType.RELEASE].version
)
def test_should_resolve_passwords():
sut = InitService(
DevopsFactory(),
BuildFileRepositoryMock(),
CredentialsApiMock(
{
"server/meissa/grafana-cloud:grafana-cloud-user": "gopass-gfc-user",
"server/meissa/grafana-cloud": "gopass-gfc-password",
}
),
EnvironmentApiMock({"C4K_GRAFANA_CLOUD_USER": "env-gfc-user"}),
)
devops = sut.initialize(devops_config({}))
c4k = devops.specialized_builds[BuildType.C4K]
assert {
"mon-auth": {
"grafana-cloud-password": "gopass-gfc-password",
"grafana-cloud-user": "env-gfc-user",
}
} == c4k.auth()