def test_login_registries(self, login_mock):
     builder = DockerBuilder(build_context='.',
                             image_name='image',
                             image_tag='tag',
                             registries=[
                                 UriSpec('user', 'pwd', 'host'),
                                 UriSpec('user', 'pwd', 'host')
                             ])
     builder.login_private_registries()
     assert login_mock.call_count == 2
 def test_build_login(self, login_mock, build_mock):
     build(build_context='.',
           image_tag='image_tag',
           image_name='image_name',
           nocache=True,
           registries=[
               UriSpec('user', 'pwd', 'host'),
               UriSpec('user', 'pwd', 'host')
           ])
     assert login_mock.call_count == 2
     assert build_mock.call_count == 1
    def test_validate_registries(self):
        with self.assertRaises(BuildException):
            DockerBuilder(build_context='.',
                          image_name='image',
                          image_tag='tag',
                          registries='foo')

        with self.assertRaises(BuildException):
            DockerBuilder(build_context='.',
                          image_name='image',
                          image_tag='tag',
                          registries=['foo',
                                      UriSpec('user', 'pwd', 'host')])

        builder = DockerBuilder(build_context='.',
                                image_name='image',
                                image_tag='tag',
                                registries=[UriSpec('user', 'pwd', 'host')])

        assert builder.registries is not None
Beispiel #4
0
def parse_uri_spec(uri_spec):
    parts = uri_spec.split('@')
    if len(parts) != 2:
        raise RheaError(
            'Received invalid uri_spec `{}`. '
            'The uri must be in the format `user:pass@host`'.format(uri_spec))

    user_pass, host = parts
    user_pass = user_pass.split(':')
    if len(user_pass) != 2:
        raise RheaError(
            'Received invalid uri_spec `{}`. `user:host` is not conform.'
            'The uri must be in the format `user:pass@host`'.format(uri_spec))

    return UriSpec(user=user_pass[0], password=user_pass[1], host=host)
Beispiel #5
0
def get_external_registries():
    registries = []
    for key in config.keys_startswith(PRIVATE_REGISTRIES_PREFIX):

        try:
            registry_dict = config.get_dict(key, is_secret=True)
            registry_spec = UriSpec(**registry_dict)
        except RheaError:
            registry_spec = config.get_string(key, is_secret=True)
            try:
                # We might get this value from a chart with `toJson` applied.
                registry_spec = json.loads(registry_spec)
            except json.decoder.JSONDecodeError:
                pass

            registry_spec = rhea_parser.parse_uri_spec(registry_spec)

        if registry_spec:
            registries.append(registry_spec)

    return registries
Beispiel #6
0
    def test_get_uri(self):
        value = self.config.get_uri('uri_key_1')
        self.assertEqual(value, UriSpec("user", "pass", "siteweb.ca"))

        value = self.config.get_uri('uri_key_2')
        self.assertEqual(value, UriSpec("user2", "pass", "localhost:8080"))

        value = self.config.get_uri('uri_key_3')
        self.assertEqual(value, UriSpec("user2", "pass", "https://quay.io"))

        value = self.config.get_uri('uri_list_key_1', is_list=True)
        self.assertEqual(value, [
            UriSpec("user", "pass", "siteweb.ca"),
            UriSpec("user2", "pass", "localhost:8080"),
            UriSpec("user2", "pass", "https://quay.io")
        ])

        with self.assertRaises(RheaError):
            self.config.get_uri('uri_error_key_1')

        with self.assertRaises(RheaError):
            self.config.get_uri('uri_error_key_2')

        with self.assertRaises(RheaError):
            self.config.get_uri('uri_error_key_3')

        with self.assertRaises(RheaError):
            self.config.get_uri('uri_error_key_4')

        with self.assertRaises(RheaError):
            self.config.get_uri('uri_list_key_1')

        with self.assertRaises(RheaError):
            self.config.get_uri('uri_list_error_key_1', is_list=True)

        with self.assertRaises(RheaError):
            self.config.get_uri('uri_list_error_key_2', is_list=True)

        with self.assertRaises(RheaError):
            self.config.get_uri('uri_list_error_key_3', is_list=True)

        with self.assertRaises(RheaError):
            self.config.get_uri('uri_list_error_key_4', is_list=True)

        with self.assertRaises(RheaError):
            self.config.get_uri('uri_key_1', is_list=True)

        with self.assertRaises(RheaError):
            self.config.get_uri('uri_non_existing_key')

        with self.assertRaises(RheaError):
            self.config.get_uri('uri_non_existing_key', is_list=True)

        self.assertEqual(
            self.config.get_uri('uri_non_existing_key', is_optional=True),
            None)
        self.assertEqual(
            self.config.get_uri('uri_non_existing_key',
                                is_optional=True,
                                default=UriSpec("user2", "pass",
                                                "localhost:8080")),
            UriSpec("user2", "pass", "localhost:8080"))

        self.assertEqual(
            self.config.get_uri('uri_non_existing_key',
                                is_list=True,
                                is_optional=True), None)
        self.assertEqual(
            self.config.get_uri('uri_non_existing_key',
                                is_list=True,
                                is_optional=True,
                                default=[
                                    UriSpec("user", "pass", "siteweb.ca"),
                                    UriSpec("user2", "pass", "localhost:8080")
                                ]), [
                                    UriSpec("user", "pass", "siteweb.ca"),
                                    UriSpec("user2", "pass", "localhost:8080")
                                ])
Beispiel #7
0
]

if os.path.isfile('{}/local.json'.format(ENV_VARS_DIR)):
    config_values.append('{}/local.json'.format(ENV_VARS_DIR))

config = rhea.Rhea.read_configs(config_values)

REGISTRY_USER = config.get_string('POLYAXON_REGISTRY_USER', is_optional=True)
REGISTRY_PASSWORD = config.get_string('POLYAXON_REGISTRY_PASSWORD',
                                      is_secret=True,
                                      is_optional=True)
REGISTRY_URI = config.get_string('POLYAXON_REGISTRY_URI', is_optional=True)

INTERNAL_REGISTRY = None
if all([REGISTRY_USER, REGISTRY_PASSWORD, REGISTRY_URI]):
    INTERNAL_REGISTRY = UriSpec(REGISTRY_USER, REGISTRY_PASSWORD, REGISTRY_URI)

# This is duplicated
PRIVATE_REGISTRIES_PREFIX = 'POLYAXON_PRIVATE_REGISTRY_'


def get_external_registries():
    registries = []
    for key in config.keys_startswith(PRIVATE_REGISTRIES_PREFIX):

        try:
            registry_dict = config.get_dict(key, is_secret=True)
            registry_spec = UriSpec(**registry_dict)
        except RheaError:
            registry_spec = config.get_string(key, is_secret=True)
            try:
Beispiel #8
0
    def test_get_uri(self):
        value = rhea_parser.get_uri(key='uri_key_1',
                                    value="user:[email protected]")
        self.assertEqual(value, UriSpec("user", "pass", "siteweb.ca"))

        value = rhea_parser.get_uri(key='uri_key_2',
                                    value="user2:pass@localhost:8080")
        self.assertEqual(value, UriSpec("user2", "pass", "localhost:8080"))

        value = rhea_parser.get_uri(key='uri_key_3',
                                    value="user2:pass@https://quay.io")
        self.assertEqual(value, UriSpec("user2", "pass", "https://quay.io"))

        value = rhea_parser.get_uri(key='uri_list_key_1',
                                    value=[
                                        "user:[email protected]",
                                        "user2:pass@localhost:8080",
                                        "user2:pass@https://quay.io"
                                    ],
                                    is_list=True)
        self.assertEqual(value, [
            UriSpec("user", "pass", "siteweb.ca"),
            UriSpec("user2", "pass", "localhost:8080"),
            UriSpec("user2", "pass", "https://quay.io")
        ])

        with self.assertRaises(RheaError):
            rhea_parser.get_uri(key='uri_error_key_1', value="foo")

        with self.assertRaises(RheaError):
            rhea_parser.get_uri(key='uri_error_key_2', value=1)

        with self.assertRaises(RheaError):
            rhea_parser.get_uri(key='uri_error_key_3', value=False)

        with self.assertRaises(RheaError):
            rhea_parser.get_uri(key='uri_error_key_4', value=["1", "foo"])

        with self.assertRaises(RheaError):
            rhea_parser.get_uri(key='uri_list_key_1',
                                value=[
                                    "user:[email protected]",
                                    "user2:pass@localhost:8080",
                                    "user2:pass@https://quay.io"
                                ])

        with self.assertRaises(RheaError):
            rhea_parser.get_uri(key='uri_list_error_key_1',
                                value=["123", "user:[email protected]"],
                                is_list=True)

        with self.assertRaises(RheaError):
            rhea_parser.get_uri(key='uri_list_error_key_2',
                                value=["user:[email protected]", 12.3],
                                is_list=True)

        with self.assertRaises(RheaError):
            rhea_parser.get_uri(key='uri_list_error_key_3',
                                value=["user:[email protected]", None],
                                is_list=True)

        with self.assertRaises(RheaError):
            rhea_parser.get_uri(key='uri_list_error_key_4',
                                value=["user:[email protected]", "123", False],
                                is_list=True)

        with self.assertRaises(RheaError):
            rhea_parser.get_uri(key='uri_key_1',
                                value="user:[email protected]",
                                is_list=True)

        with self.assertRaises(RheaError):
            rhea_parser.get_uri(key='uri_non_existing_key', value=None)

        with self.assertRaises(RheaError):
            rhea_parser.get_uri(key='uri_non_existing_key',
                                value=NO_VALUE_FOUND)

        with self.assertRaises(RheaError):
            rhea_parser.get_uri(key='uri_non_existing_key',
                                value=None,
                                is_list=True)

        self.assertEqual(
            rhea_parser.get_uri(key='uri_non_existing_key',
                                value=None,
                                is_optional=True), None)
        self.assertEqual(
            rhea_parser.get_uri(key='uri_non_existing_key',
                                value=None,
                                is_optional=True,
                                default=UriSpec("user2", "pass",
                                                "localhost:8080")),
            UriSpec("user2", "pass", "localhost:8080"))

        self.assertEqual(
            rhea_parser.get_uri(key='uri_non_existing_key',
                                value=None,
                                is_list=True,
                                is_optional=True), None)
        self.assertEqual(
            rhea_parser.get_uri(key='uri_non_existing_key',
                                value=None,
                                is_list=True,
                                is_optional=True,
                                default=[
                                    UriSpec("user", "pass", "siteweb.ca"),
                                    UriSpec("user2", "pass", "localhost:8080")
                                ]), [
                                    UriSpec("user", "pass", "siteweb.ca"),
                                    UriSpec("user2", "pass", "localhost:8080")
                                ])