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

Feature/GitHub action (#1) #2

Merged
merged 1 commit into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
38 changes: 38 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: test aws action for put parameter store
permissions:
contents: write
actions: write
on:
push:
branches:
- dev
- main
jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: test parameter store - input env
uses: ./
with:
parameters: test=test

- name: test parameter store - input multiline env
uses: ./
with:
parameters: |
test=test
test1="test1"

- name: test parameter store - input variables
uses: ./
env:
ENV_VAL: ${{ github.workspace }}
with:
parameters: |
test=test
test1="test1"
env_val=${{ env.ENV_VAL }}

1 change: 1 addition & 0 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
python 3.12.3
13 changes: 13 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FROM python:3.12.3-slim

RUN apt-get update
RUN pip install --upgrade pip

COPY requirements.txt .
RUN pip install -r requirements.txt

WORKDIR /usr/src

COPY ./src/ .

ENTRYPOINT ["python", "/usr/src/aws_ssm.py"]
15 changes: 15 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name: 'AWS SSM'
description: 'A Github action for parameter store update'
inputs:
parameters:
description: 'Provide parameters as json or .env file format with each pair for one line.'
required: false
params-file:
description: 'Write params to a json file and provide the file path from github workspace root.'
required: false
runs:
using: docker
image: Dockerfile
env:
INPUT_PARAMS: ${{ inputs.parameters }}
INPUT_PARAMS_FILE_PATH: ${{ inputs.params-file }}
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
boto3
28 changes: 28 additions & 0 deletions src/aws_ssm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import os
import pathlib

from inputs import AwsParameter, parse_input_params
from common.aws_clients import get_client


def run(aws_parameters: dict[str, AwsParameter]):
ssm_client = get_client(service_name='ssm',
region_name=os.getenv('AWS_REGION'),
aws_access_key=os.getenv('AWS_ACCESS_KEY'),
aws_secret_key=os.getenv('AWS_SECRET_KEY'),
)
for name, param in aws_parameters.items():
ssm_client.put_parameter(Name=param.name, Value=param.value, Type=param.param_type, Overwrite=param.overwrite)


if __name__ == '__main__':
input_params = os.getenv('INPUT_PARAMS')
params_inline = parse_input_params(input_params) if input_params is not None else {}
params_from_file = {}
params_file_path = os.getenv('INPUT_PARAMS_FILE_PATH')
if params_file_path is not None and params_file_path != "":
path = pathlib.PurePath(os.getenv('GITHUB_WORKSPACE'), params_file_path)
with open(path, 'r') as f:
params_from_file = parse_input_params(f.read())
params = {**params_from_file, **params_inline}
run(params)
Empty file added src/common/__init__.py
Empty file.
9 changes: 9 additions & 0 deletions src/common/aws_clients.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import os

import boto3


def get_client(service_name: str, region_name: str, aws_access_key: str, aws_secret_key: str):
return boto3.client(service_name=service_name, region_name=region_name,
aws_access_key_id=aws_access_key,
aws_secret_access_key=aws_secret_key)
48 changes: 48 additions & 0 deletions src/inputs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import json
from dataclasses import dataclass


@dataclass(init=True)
class AwsParameter:
name: str
value: str
param_type: str = 'String'
overwrite: bool = True


def ensure_json_input(input_params: str):
try:
return json.loads(input_params)
except ValueError as e:
print('Not valid JSON')
return None


def env_to_param(env_line: str) -> AwsParameter:
env_line = env_line.strip()
env_line = env_line.split(sep="=", maxsplit=1)
return AwsParameter(name=env_line[0], value=env_line[1])


def ensure_env_input(input_params: str):
param_lines = input_params.strip().split(sep="\n")
return [param_line.strip() for param_line in param_lines]


def value_to_aws_parameter(param_name, param_value):
parsed_value_json = ensure_json_input(param_value)
if parsed_value_json is None:
return AwsParameter(name=param_name, value=param_value)
value = parsed_value_json.get('value') if parsed_value_json.get('value') is not None else None
param_type = parsed_value_json.get('type') if parsed_value_json.get('type') is not None else 'String'
overwrite = parsed_value_json.get('overwrite') if parsed_value_json.get('overwrite') is not None else True
return AwsParameter(name=param_name, value=value, param_type=param_type, overwrite=overwrite)


def parse_input_params(input_params: str):
parsed_params_json = ensure_json_input(input_params)
if parsed_params_json is not None:
return {key: value_to_aws_parameter(key, value) for key, value in parsed_params_json.items()}
parsed_params_env = ensure_env_input(input_params)
parsed_aws_params_env = [env_to_param(env_param) for env_param in parsed_params_env]
return {p.name: p for p in parsed_aws_params_env}