Esempio n. 1
0
def test_credential_get_input(organization_factory):
    organization = organization_factory('test').organization
    type_ = CredentialType(kind='vault',
                           name='somevault',
                           managed_by_tower=True,
                           inputs={
                               'fields': [{
                                   'id': 'vault_password',
                                   'type': 'string',
                                   'secret': True,
                               }, {
                                   'id': 'vault_id',
                                   'type': 'string',
                                   'secret': False
                               }, {
                                   'id': 'secret',
                                   'type': 'string',
                                   'secret': True,
                               }]
                           })
    type_.save()

    cred = Credential(organization=organization,
                      credential_type=type_,
                      name="Bob's Credential",
                      inputs={'vault_password': '******'})
    cred.save()
    cred.full_clean()

    assert isinstance(cred, Credential)
    # verify expected exception is raised when attempting to access an unset
    # input without providing a default
    with pytest.raises(AttributeError):
        cred.get_input('vault_id')
    # verify that the provided default is used for unset inputs
    assert cred.get_input('vault_id', default='foo') == 'foo'
    # verify expected exception is raised when attempting to access an undefined
    # input without providing a default
    with pytest.raises(AttributeError):
        cred.get_input('field_not_on_credential_type')
    # verify that the provided default is used for undefined inputs
    assert cred.get_input('field_not_on_credential_type',
                          default='bar') == 'bar'
    # verify expected exception is raised when attempting to access an unset secret
    # input without providing a default
    with pytest.raises(AttributeError):
        cred.get_input('secret')
    # verify that the provided default is used for undefined inputs
    assert cred.get_input('secret', default='fiz') == 'fiz'
    # verify return values for encrypted secret fields are decrypted
    assert cred.inputs['vault_password'].startswith('$encrypted$')
    assert cred.get_input('vault_password') == 'testing321'
def test_cred_type_input_schema_validity(input_, valid):
    type_ = CredentialType(
        kind='cloud',
        name='SomeCloud',
        managed_by_tower=True,
        inputs=input_
    )
    field = CredentialType._meta.get_field('inputs')
    if valid is False:
        with pytest.raises(ValidationError):
            field.clean(input_, type_)
    else:
        field.clean(input_, type_)
Esempio n. 3
0
def test_cred_type_input_schema_validity(input_, valid):
    type_ = CredentialType(
        kind='cloud',
        name='SomeCloud',
        managed_by_tower=True,
        inputs=input_
    )
    if valid is False:
        with pytest.raises(Exception) as e:
            type_.full_clean()
        assert e.type in (ValidationError, serializers.ValidationError)
    else:
        type_.full_clean()
Esempio n. 4
0
def test_credential_creation(organization_factory):
    org = organization_factory('test').organization
    type_ = CredentialType(
        kind='cloud', name='SomeCloud', managed_by_tower=True, inputs={'fields': [{'id': 'username', 'label': 'Username for SomeCloud', 'type': 'string'}]}
    )
    type_.save()

    cred = Credential(credential_type=type_, name="Bob's Credential", inputs={'username': '******'}, organization=org)
    cred.save()
    cred.full_clean()
    assert isinstance(cred, Credential)
    assert cred.name == "Bob's Credential"
    assert cred.inputs['username'] == 'bob'
def test_custom_cred_with_empty_encrypted_field():
    ct = CredentialType(name='My Custom Cred',
                        kind='custom',
                        inputs={
                            'fields': [{
                                'id': 'some_field',
                                'label': 'My Field',
                                'secret': True
                            }]
                        })
    cred = Credential(id=4,
                      name='Testing 1 2 3',
                      credential_type=ct,
                      inputs={})
    assert cred.encrypt_field('some_field', None) is None
def test_credential_creation_validation_failure(inputs):
    type_ = CredentialType(
        kind='cloud',
        name='SomeCloud',
        managed_by_tower=True,
        inputs={
            'fields': [{'id': 'username', 'label': 'Username for SomeCloud', 'type': 'string'}, {'id': 'flag', 'label': 'Some Boolean Flag', 'type': 'boolean'}]
        },
    )
    cred = Credential(credential_type=type_, name="Bob's Credential", inputs=inputs)
    field = cred._meta.get_field('inputs')

    with pytest.raises(Exception) as e:
        field.validate(inputs, cred)
    assert e.type in (ValidationError, DRFValidationError)
Esempio n. 7
0
def test_cred_type_injectors_schema(injectors, valid):
    type_ = CredentialType(
        kind='cloud',
        name='SomeCloud',
        managed_by_tower=True,
        inputs={
            'fields': [
                {'id': 'username', 'type': 'string', 'label': '_'},
                {'id': 'pass', 'type': 'string', 'label': '_'},
                {'id': 'awx_secret', 'type': 'string', 'label': '_'},
                {'id': 'host', 'type': 'string', 'label': '_'},
            ]
        },
        injectors=injectors
    )
    if valid is False:
        with pytest.raises(ValidationError):
            type_.full_clean()
    else:
        type_.full_clean()
def test_cred_type_injectors_schema(injectors, valid):
    type_ = CredentialType(
        kind='cloud',
        name='SomeCloud',
        managed_by_tower=True,
        inputs={
            'fields': [
                {'id': 'username', 'type': 'string', 'label': '_'},
                {'id': 'pass', 'type': 'string', 'label': '_'},
                {'id': 'awx_secret', 'type': 'string', 'label': '_'},
                {'id': 'host', 'type': 'string', 'label': '_'},
            ]
        },
        injectors=injectors
    )
    field = CredentialType._meta.get_field('injectors')
    if valid is False:
        with pytest.raises(ValidationError, message="Injector was supposed to throw a validation error, data: {}".format(injectors)):
            field.clean(injectors, type_)
    else:
        field.clean(injectors, type_)
Esempio n. 9
0
def test_cred_type_injectors_schema(injectors, valid):
    type_ = CredentialType(
        kind='cloud',
        name='SomeCloud',
        managed=True,
        inputs={
            'fields': [
                {'id': 'username', 'type': 'string', 'label': '_'},
                {'id': 'pass', 'type': 'string', 'label': '_'},
                {'id': 'awx_secret', 'type': 'string', 'label': '_'},
                {'id': 'host', 'type': 'string', 'label': '_'},
            ]
        },
        injectors=injectors,
    )
    field = CredentialType._meta.get_field('injectors')
    if valid is False:
        with pytest.raises(ValidationError):
            field.clean(injectors, type_)
    else:
        field.clean(injectors, type_)