introduce Credentials

This commit is contained in:
Michael Jerger 2023-05-16 18:13:33 +02:00
parent 3cfb60a2de
commit b72e7e717e
5 changed files with 94 additions and 11 deletions

View file

@ -34,23 +34,30 @@ class CredentialMapping(Validateable):
def name_for_input(self): def name_for_input(self):
if self.name: if self.name:
return self.name result = self.name
elif self.gopass_field:
result = underscore(self.gopass_field)
else: else:
return underscore(self.gopass_field) result = ""
return result
def name_for_environment(self): def name_for_environment(self):
return self.name_for_input().upper() return self.name_for_input().upper()
class Credentials(Validateable): class Credentials(Validateable):
def __init__(self, input: dict): def __init__(self, input: dict, default_mappings: list = []):
input_mappings = input.get("credentials_mapping", []) input_mappings = input.get("credentials_mapping", [])
self.mappings = [] self.mappings = {}
for input_mapping in default_mappings:
mapping = CredentialMapping(input_mapping)
self.mappings[mapping.name_for_input()] = mapping
for input_mapping in input_mappings: for input_mapping in input_mappings:
self.mappings.append(CredentialMapping(input_mapping)) mapping = CredentialMapping(input_mapping)
self.mappings[mapping.name_for_input()] = mapping
def validate(self) -> List[str]: def validate(self) -> List[str]:
result = [] result = []
for mapping in self.mappings: for mapping in self.mappings.values():
result += mapping.validate() result += mapping.validate()
return result return result

View file

@ -0,0 +1,28 @@
from pathlib import Path
from .common import Devops, MixinType
from .devops_factory import DevopsFactory
from .version import Version
from src.main.python.ddadevops.infrastructure import (
BuildFileRepository
)
class CredentialsService:
def __init__(self, gopass_api, environment_api):
@classmethod
def prod(cls):
return cls(
DevopsFactory(),
BuildFileRepository(base_dir),
)
def initialize(self, input: dict) -> Devops:
mixin_types = self.devops_factory.__parse_mixin_types__(input["mixin_types"])
version = None
if MixinType.RELEASE in mixin_types:
primary_build_file_id = input.get("release_primary_build_file", "./project.clj")
primary_build_file = self.build_file_repository.get(Path(primary_build_file_id))
version = primary_build_file.get_version()
return self.devops_factory.build_devops(input, version=version)

View file

@ -37,7 +37,7 @@ class DevopsFactory:
"name": "grafana_cloud_password", "name": "grafana_cloud_password",
} }
] ]
mixins[MixinType.CREDENTIALS] = Credentials(input, version) mixins[MixinType.CREDENTIALS] = Credentials(input, default_mappings)
devops = Devops(input, specialized_builds=specialized_builds, mixins=mixins) devops = Devops(input, specialized_builds=specialized_builds, mixins=mixins)

View file

@ -99,3 +99,8 @@ class ExecutionApi:
output = execute(command, True) output = execute(command, True)
print(output) print(output)
return output return output
class EnvironmentApi():
def get(self, key):
return environ.get(key)

View file

@ -71,8 +71,53 @@ def test_should_create_credentials():
], ],
} }
) )
assert sut assert sut
assert 2 == len(sut.mappings)
sut = Credentials(
{},
default_mappings=[
{
"gopass_path": "server/meissa/grafana-cloud",
"gopass_field": "grafana-cloud-user",
},
{
"gopass_path": "server/meissa/grafana-cloud",
"name": "grafana_cloud_password",
},
],
)
assert sut
assert 2 == len(sut.mappings)
sut = Credentials(
{
"credentials_mapping": [
{
"gopass_path": "dome/path",
"gopass_field": "some-field",
},
{
"gopass_path": "another_path",
"name": "grafana_cloud_password",
},
],
},
default_mappings=[
{
"gopass_path": "server/meissa/grafana-cloud",
"gopass_field": "grafana-cloud-user",
},
{
"gopass_path": "server/meissa/grafana-cloud",
"name": "grafana_cloud_password",
},
],
)
assert sut
assert 3 == len(sut.mappings)
assert sut.mappings["grafana_cloud_password"].gopass_path == "another_path"
def test_should_validate_credentials(): def test_should_validate_credentials():
sut = Credentials( sut = Credentials(
@ -98,9 +143,7 @@ def test_should_validate_credentials():
"gopass_path": "server/meissa/grafana-cloud", "gopass_path": "server/meissa/grafana-cloud",
"gopass_field": "grafana-cloud-user", "gopass_field": "grafana-cloud-user",
}, },
{ {"gopass_path": "server/meissa/grafana-cloud"},
"gopass_path": "server/meissa/grafana-cloud"
},
], ],
} }
) )