Skip to content

Commit

Permalink
Merge pull request #44 from cloudblue/LITE-29926-support-sas-token
Browse files Browse the repository at this point in the history
LITE-29926 Support sas token
  • Loading branch information
maxipavlovic authored Apr 8, 2024
2 parents 986e603 + 04f232a commit 5d67a5a
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 26 deletions.
8 changes: 4 additions & 4 deletions connect_bi_reporter/credentials/api/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

class CredentialCreateSchema(NonNullSchema):
name: str
connection_string: str
sas_token: str


class CredentialListSchema(ReferenceSchema):
Expand All @@ -15,18 +15,18 @@ class CredentialListSchema(ReferenceSchema):

class CredentialUpdateSchema(NonNullSchema):
name: Optional[str]
connection_string: Optional[str]
sas_token: Optional[str]


class CredentialGetSchema(CredentialListSchema):
connection_string: str
sas_token: str


def map_to_credential_get_schema(credential):
return CredentialGetSchema(
id=credential.id,
name=credential.name,
connection_string=credential.connection_string,
sas_token=credential.sas_token,
owner={'id': credential.account_id},
events={
'created': {'at': credential.created_at, 'by': {'id': credential.created_by}},
Expand Down
2 changes: 1 addition & 1 deletion connect_bi_reporter/credentials/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Credential(Model):

id = db.Column(db.String(20), primary_key=True)
name = db.Column(db.String(64), nullable=False)
connection_string = db.Column(db.String(256), nullable=False)
sas_token = db.Column(db.String(256), nullable=False)
account_id = db.Column(db.String(20), nullable=False)
created_at = db.Column(db.DateTime(), default=datetime.utcnow)
created_by = db.Column(db.String(20))
Expand Down
6 changes: 5 additions & 1 deletion connect_bi_reporter/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@
'initial_value': 'extension-bi-reporter',
'secure': False,
},
{
'name': 'UPLOADS_ACCOUNT_NAME',
'initial_value': 'extension-local',
'secure': False,
},
{
'name': 'DATABASE_URL',
'initial_value': 'postgresql+psycopg2://postgres:1q2w3e@db/bi_reporter',
Expand Down
9 changes: 8 additions & 1 deletion connect_bi_reporter/uploads/storage_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,20 @@
from azure.storage.blob import ContainerClient


CONNECTION_STRING_TMPL = 'DefaultEndpointsProtocol=https;AccountName={account_name}'


def upload_file(data, filename, credentials, logger, config): # pragma: no cover
account_name = config.get('UPLOADS_ACCOUNT_NAME')
container_name = config.get('UPLOADS_CONTAINER_NAME')

try:
container_client = ContainerClient.from_connection_string(
credentials.connection_string, container_name,
CONNECTION_STRING_TMPL.format(account_name=account_name),
container_name=container_name,
credential=credentials.sas_token,
)

except ResourceNotFoundError:
logger.error(f'Error uploading file: Containter {container_name} does not exist.')

Expand Down
8 changes: 4 additions & 4 deletions tests/credentials/api/test_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
def test_credentials_create_schema():
serializer = CredentialCreateSchema(
name='Creating credentials',
connection_string="D=https;AccountName=AN;AccountKey=AK;EndpointSuffix=core.windows.net",
sas_token="D=https;AccountName=AN;AccountKey=AK;EndpointSuffix=core.windows.net",
)
assert serializer.dict() == {
'name': 'Creating credentials',
'connection_string': "D=https;AccountName=AN;AccountKey=AK;EndpointSuffix=core.windows.net",
'sas_token': "D=https;AccountName=AN;AccountKey=AK;EndpointSuffix=core.windows.net",
}


Expand Down Expand Up @@ -41,7 +41,7 @@ def test_credentials_get_schema(credential_factory):
serializer = CredentialGetSchema(
id=credential.id,
name=credential.name,
connection_string=credential.connection_string,
sas_token=credential.sas_token,
owner={'id': credential.account_id},
events={
'created': {'at': credential.created_at, 'by': {'id': credential.created_by}},
Expand All @@ -52,7 +52,7 @@ def test_credentials_get_schema(credential_factory):
assert serializer.dict() == {
'id': credential.id,
'name': credential.name,
'connection_string': credential.connection_string,
'sas_token': credential.sas_token,
'owner': {'id': credential.account_id},
'events': {
'created': {'at': credential.created_at, 'by': {'id': credential.created_by}},
Expand Down
18 changes: 9 additions & 9 deletions tests/credentials/api/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@


def test_create_credential(installation, api_client, connect_auth_header):
body = {'name': 'My credentials', 'connection_string': 'core.windows.net'}
body = {'name': 'My credentials', 'sas_token': 'core.windows.net'}
response = api_client.post(
'/api/credentials',
installation=installation,
Expand All @@ -16,7 +16,7 @@ def test_create_credential(installation, api_client, connect_auth_header):
response_data = response.json()
assert response_data['id'] is not None
assert response_data['name'] == body['name']
assert response_data['connection_string'] == body['connection_string']
assert response_data['sas_token'] == body['sas_token']
assert response_data['owner']['id'] == installation['owner']['id']
events = response_data['events']
assert events['created']['at'] is not None
Expand All @@ -25,11 +25,11 @@ def test_create_credential(installation, api_client, connect_auth_header):
assert events['updated']['by'] is not None


@pytest.mark.parametrize('remove_field', ('name', 'connection_string'))
@pytest.mark.parametrize('remove_field', ('name', 'sas_token'))
def test_create_credential_mandatory_fields(
remove_field, installation, api_client, connect_auth_header,
):
body = {'name': 'My credentials', 'connection_string': 'core.windows.net'}
body = {'name': 'My credentials', 'sas_token': 'core.windows.net'}
del body[remove_field]

response = api_client.post(
Expand Down Expand Up @@ -112,7 +112,7 @@ def test_get_credential(installation, api_client, connect_auth_header, credentia
response_data = response.json()
assert response_data['id'] == credential.id
assert response_data['name'] == credential.name
assert response_data['connection_string'] == credential.connection_string
assert response_data['sas_token'] == credential.sas_token
assert response_data['owner']['id'] == installation['owner']['id']
events = response_data['events']
assert events['created']['at'] is not None
Expand Down Expand Up @@ -146,7 +146,7 @@ def test_update_credential(
):
cred = credential_factory(account_id=installation['owner']['id'])
updated_at = cred.updated_at
body = {'name': 'My credentials', 'connection_string': 'core.windows.net'}
body = {'name': 'My credentials', 'sas_token': 'core.windows.net'}
response = api_client.put(
f'/api/credentials/{cred.id}',
installation=installation,
Expand All @@ -159,7 +159,7 @@ def test_update_credential(
response_data = response.json()
assert response_data['id'] is not None
assert response_data['name'] == body['name']
assert response_data['connection_string'] == body['connection_string']
assert response_data['sas_token'] == body['sas_token']
assert response_data['owner']['id'] == installation['owner']['id']
events = response_data['events']
assert events['created']['at'] == cred.created_at.isoformat()
Expand All @@ -171,7 +171,7 @@ def test_update_credential(


def test_update_credential_404(installation, api_client, connect_auth_header):
body = {'name': 'My credentials', 'connection_string': 'core.windows.net'}
body = {'name': 'My credentials', 'sas_token': 'core.windows.net'}
response = api_client.put(
'/api/credentials/NOT-FOUND',
installation=installation,
Expand Down Expand Up @@ -206,7 +206,7 @@ def test_update_credential_nothing_to_update(
response_data = response.json()
assert response_data['id'] is not None
assert response_data['name'] == cred.name
assert response_data['connection_string'] == cred.connection_string
assert response_data['sas_token'] == cred.sas_token
assert response_data['owner']['id'] == installation['owner']['id']
events = response_data['events']
assert events['created']['at'] is not None
Expand Down
4 changes: 2 additions & 2 deletions tests/credentials/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ def test_create_credential(dbsession, credential_factory):
credential = credential_model(
name='My cred',
account_id='PA-000-000',
connection_string='core.windows.net',
sas_token='core.windows.net',
)
dbsession.add_with_verbose(credential)
dbsession.commit()
dbsession.refresh(credential)
assert credential.id.startswith(credential_model.PREFIX)
assert credential.name == 'My cred'
assert credential.connection_string == 'core.windows.net'
assert credential.sas_token == 'core.windows.net'
assert isinstance(credential.created_at, datetime)


Expand Down
8 changes: 4 additions & 4 deletions tests/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ class Meta:

id = factory.Sequence(lambda _: _generate_verbose_id(Credential.PREFIX))
name = factory.Sequence(lambda n: f'Credential {n}')
connection_string = factory.Sequence(
lambda n: (
f'DefaultEndpointsProtocol=https;AccountName={n};'
f'AccountKey={n};EndpointSuffix=core.windows.net'
sas_token = factory.Sequence(
lambda _: (
'sv=2022-11-02&ss=bfqt&srt=co&sp=rwdlacupiytfx&se=2024-04-09T19:38:51Z'
'&st=2024-04-08T11:38:51Z&spr=https&sig=MeZjy2Df96DjJax7Oeor8ZzUePjhTzaVZRvE1wixThM%3D'
),
)
account_id = factory.Sequence(lambda n: f'PA-{n}')
Expand Down

0 comments on commit 5d67a5a

Please sign in to comment.