Skip to content
Closed
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
41 changes: 40 additions & 1 deletion src/api/handlers/projects/secrets.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from pyinfrabox.utils import validate_uuid
from pyinfraboxutils.ibflask import OK
from pyinfraboxutils.ibrestplus import api, response_model
from pyinfraboxutils.secrets import encrypt_secret
from pyinfraboxutils.secrets import encrypt_secret, decrypt_secret

ns = api.namespace('Secrets',
path='/api/v1/projects/<project_id>/secrets',
Expand Down Expand Up @@ -110,3 +110,42 @@ def delete(self, project_id, secret_id):
g.db.commit()

return OK('Successfully deleted secret.')


secret_value_model = api.model('SecretValue', {
'name': fields.String(required=True),
'value': fields.String(required=True),
})


@ns.route('/values')
@api.doc(responses={403: 'Not Authorized', 404: 'Project not found'})
class SecretValues(Resource):

@api.marshal_list_with(secret_value_model)
def get(self, project_id):
'''
Returns project's secrets with decrypted values.

WARNING: this endpoint exposes plaintext secret values and is
restricted to project administrators by the OPA policy.
'''
if not validate_uuid(project_id):
abort(400, 'Invalid project uuid.')

project = g.db.execute_one_dict('''
SELECT id FROM project WHERE id = %s
''', [project_id])

if not project:
abort(404, 'Project not found.')

secrets = g.db.execute_many_dict('''
SELECT name, value FROM secret
WHERE project_id = %s
''', [project_id])

for secret in secrets:
secret['value'] = decrypt_secret(secret['value'])

return secrets
8 changes: 8 additions & 0 deletions src/openpolicyagent/policies/projects_secrets.rego
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ allow {
projects_secrets_administrator([api.token.user.id, project_id])
}

# Allow GET access to /api/v1/projects/<project_id>/secrets/values for project administrators
allow {
api.method = "GET"
api.path = ["api", "v1", "projects", project_id, "secrets", "values"]
api.token.type = "user"
projects_secrets_administrator([api.token.user.id, project_id])
}

# Allow POST access to /api/v1/projects/<project_id>/secrets for project administrators
allow {
api.method = "POST"
Expand Down
Loading