Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API Key implementation #149

Merged
merged 2 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions artifactory_cleanup/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,17 @@ def main(self):
print(str(err), file=sys.stderr)
sys.exit(1)

server, user, password = loader.get_connection()
server, user, password, apikey = loader.get_connection()
session = BaseUrlSession(server)
session.auth = HTTPBasicAuth(user, password)
if apikey != "":
print("Using API Key")
headers = {
"X-JFrog-Art-Api": apikey
}
session.headers = headers
else:
print("Using user and password")
session.auth = HTTPBasicAuth(user, password)
allburov marked this conversation as resolved.
Show resolved Hide resolved

self._destroy_or_verbose()
print(f"Using {self._worker_count} workers")
Expand Down
17 changes: 10 additions & 7 deletions artifactory_cleanup/loaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,11 @@ def get_root_schema(self, rules):
config_schema = cfgv.Map(
"Config",
None,
cfgv.NoAdditionalKeys(["server", "user", "password", "policies"]),
cfgv.NoAdditionalKeys(["server", "user", "password", "policies", "apikey"]),
cfgv.Required("server", cfgv.check_string),
cfgv.Required("user", cfgv.check_string),
cfgv.Required("password", cfgv.check_string),
# User and password required, if apikey missing
cfgv.Conditional("user", cfgv.check_string, "apikey", cfgv.MISSING, False),
cfgv.Conditional("password", cfgv.check_string, "apikey", cfgv.MISSING, False),
cfgv.RequiredRecurse("policies", cfgv.Array(policy_schema)),
)

Expand Down Expand Up @@ -184,16 +185,18 @@ def load(filename):
filename, schema, yaml.safe_load, InvalidConfigError
)

def get_connection(self) -> Tuple[str, str, str]:
def get_connection(self) -> Tuple[str, str, str, str]:
config = self.load(self.filepath)
server = config["artifactory-cleanup"]["server"]
user = config["artifactory-cleanup"]["user"]
password = config["artifactory-cleanup"]["password"]
user = config.get("artifactory-cleanup", {}).get("user", "")
password = config.get("artifactory-cleanup", {}).get("password", "")
apikey = config.get("artifactory-cleanup", {}).get("apikey", "")

server = os.path.expandvars(server)
user = os.path.expandvars(user)
password = os.path.expandvars(password)
return server, user, password
apikey = os.path.expandvars(apikey)
return server, user, password, apikey


class PythonLoader:
Expand Down
1 change: 1 addition & 0 deletions tests/data/all-built-in-rules.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ artifactory-cleanup:
server: https://repo.example.com/artifactory
user: $ARTIFACTORY_USERNAME
password: $ARTIFACTORY_PASSWORD
apikey: $ARTIFACTORY_APIKEY

policies:
- name: repo-without-name
Expand Down
4 changes: 3 additions & 1 deletion tests/test_loaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ def test_all_rules(self, shared_datadir):
def test_load_env_variables(self, shared_datadir, monkeypatch):
monkeypatch.setenv("ARTIFACTORY_USERNAME", "UserName")
monkeypatch.setenv("ARTIFACTORY_PASSWORD", "P@ssw0rd")
monkeypatch.setenv("ARTIFACTORY_APIKEY", "Ap1Key")

loader = YamlConfigLoader(shared_datadir / "all-built-in-rules.yaml")
server, user, password = loader.get_connection()
server, user, password, apikey = loader.get_connection()

assert server == "https://repo.example.com/artifactory"
assert user == "UserName"
assert password == "P@ssw0rd"
assert apikey == "Ap1Key"