Commit 761dde4a authored by Stavros-Anastasios Charismiadis's avatar Stavros-Anastasios Charismiadis
Browse files

Read vault hostname from initial deployment value for all CAPIF APIs, add...

Read vault hostname from initial deployment value for all CAPIF APIs, add argument in run.sh for vault host, give helper access to nginx server certificates via volume
parent 471bb00a
Loading
Loading
Loading
Loading
Loading
+65 −13
Original line number Diff line number Diff line
import os

import yaml
import re


# pattern for global vars: look for ${word}
pattern = re.compile('.*?\${(\w+)}.*?')
loader = yaml.SafeLoader


def constructor_env_variables(loader, node):
    """
    Extracts the environment variable from the node's value
    :param yaml.Loader loader: the yaml loader
    :param node: the current node in the yaml
    :return: the parsed string that contains the value of the environment
    variable
    """
    value = loader.construct_scalar(node)
    match = pattern.findall(value)  # to find all env variables in line
    if match:
        full_value = value
        for g in match:
            full_value = full_value.replace(
                f'${{{g}}}', os.environ.get(g, g)
            )
        return full_value
    return value

def parse_config(path=None, data=None, tag='!ENV'):
    """
    Load a yaml configuration file and resolve any environment variables
    The environment variables must have !ENV before them and be in this format
    to be parsed: ${VAR_NAME}.
    E.g.:
    database:
        host: !ENV ${HOST}
        port: !ENV ${PORT}
    app:
        log_path: !ENV '/var/${LOG_PATH}'
        something_else: !ENV '${AWESOME_ENV_VAR}/var/${A_SECOND_AWESOME_VAR}'
    :param str path: the path to the yaml file
    :param str data: the yaml data itself as a stream
    :param str tag: the tag to look for
    :return: the dict configuration
    :rtype: dict[str, T]
    """

    # the tag will be used to mark where to start searching for the pattern
    # e.g. somekey: !ENV somestring${MYENVVAR}blah blah blah
    loader.add_implicit_resolver(tag, pattern, None)
    loader.add_constructor(tag, constructor_env_variables)

    if path:
        with open(path) as conf_data:
            return yaml.load(conf_data, Loader=loader)
    elif data:
        return yaml.load(data, Loader=loader)
    else:
        raise ValueError('Either a path or data should be defined as input')


#Config class to get config
@@ -10,14 +68,8 @@ class Config:
        self.file="../config.yaml"
        self.my_config = {}

		stamp = os.stat(self.file).st_mtime
		if stamp != self.cached:
			self.cached = stamp
			f = open(self.file)
			self.my_config = yaml.safe_load(f)
			f.close()
        self.my_config = parse_config(path=self.file)

    def get_config(self):
        return self.my_config
+6 −6
Original line number Diff line number Diff line
@@ -9,12 +9,12 @@ mongo: {
  'port': "27017"
}

ca_factory: {
  "url": "vault",
  "port": "8200",
  "token": "dev-only-token",
  "verify": False
}
ca_factory:
  url: !ENV ${VAULT_HOSTNAME}
  port: "8200"
  token: "dev-only-token"
  verify: False


monitoring: {
  "fluent_bit_host": fluent-bit,
+1 −1
Original line number Diff line number Diff line
@@ -26,7 +26,7 @@ while [ $ATTEMPT -lt $MAX_RETRIES ]; do
    if [ -n "$RESPONSE" ] && [ "$RESPONSE" != "null" ]; then
        echo "$RESPONSE" > /usr/src/app/api_invoker_management/pubkey.pem
        echo "Public key successfully saved."
        gunicorn -k uvicorn.workers.UvicornH11Worker --bind 0.0.0.0:8080 \
        gunicorn -k uvicorn.workers.UvicornH11Worker --timeout 120 --bind 0.0.0.0:8080 \
         --chdir /usr/src/app/api_invoker_management wsgi:app
        exit 0  # Exit successfully
    else
+68 −13
Original line number Diff line number Diff line
import os

import yaml
import re


# pattern for global vars: look for ${word}
pattern = re.compile('.*?\${(\w+)}.*?')
loader = yaml.SafeLoader


def constructor_env_variables(loader, node):
    """
    Extracts the environment variable from the node's value
    :param yaml.Loader loader: the yaml loader
    :param node: the current node in the yaml
    :return: the parsed string that contains the value of the environment
    variable
    """
    value = loader.construct_scalar(node)
    match = pattern.findall(value)  # to find all env variables in line
    if match:
        full_value = value
        for g in match:
            full_value = full_value.replace(
                f'${{{g}}}', os.environ.get(g, g)
            )
        return full_value
    return value

def parse_config(path=None, data=None, tag='!ENV'):
    """
    Load a yaml configuration file and resolve any environment variables
    The environment variables must have !ENV before them and be in this format
    to be parsed: ${VAR_NAME}.
    E.g.:
    database:
        host: !ENV ${HOST}
        port: !ENV ${PORT}
    app:
        log_path: !ENV '/var/${LOG_PATH}'
        something_else: !ENV '${AWESOME_ENV_VAR}/var/${A_SECOND_AWESOME_VAR}'
    :param str path: the path to the yaml file
    :param str data: the yaml data itself as a stream
    :param str tag: the tag to look for
    :return: the dict configuration
    :rtype: dict[str, T]
    """

    # the tag will be used to mark where to start searching for the pattern
    # e.g. somekey: !ENV somestring${MYENVVAR}blah blah blah
    loader.add_implicit_resolver(tag, pattern, None)
    loader.add_constructor(tag, constructor_env_variables)

    if path:
        with open(path) as conf_data:
            return yaml.load(conf_data, Loader=loader)
    elif data:
        return yaml.load(data, Loader=loader)
    else:
        raise ValueError('Either a path or data should be defined as input')


#Config class to get config
@@ -9,12 +67,9 @@ class Config:
        self.cached = 0
        self.file="../config.yaml"
        self.my_config = {}
		stamp = os.stat(self.file).st_mtime
		if stamp != self.cached:
			self.cached = stamp
			f = open(self.file)
			self.my_config = yaml.safe_load(f)
			f.close()

        self.my_config = parse_config(path=self.file)

    def get_config(self):
        return self.my_config
+6 −6
Original line number Diff line number Diff line
@@ -9,12 +9,12 @@ mongo: {
}


ca_factory: {
  "url": "vault",
  "port": "8200",
  "token": "dev-only-token",
  "verify": False
}
ca_factory:
  url: !ENV ${VAULT_HOSTNAME}
  port: "8200"
  token: "dev-only-token"
  verify: False



monitoring: {
Loading