Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: twingateresourceaccess - allow specifying principal by name (#62)
- Loading branch information
Showing
13 changed files
with
589 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import logging | ||
|
||
from gql import gql | ||
from gql.transport.exceptions import TransportQueryError | ||
|
||
from app.api.protocol import TwingateClientProtocol | ||
|
||
QUERY_GET_GROUP_ID_BY_NAME = gql( | ||
""" | ||
query GetGroupByName($name: String!) { | ||
groups(filter: {name: {eq: $name}}) { | ||
edges { | ||
node { | ||
id | ||
name | ||
} | ||
} | ||
} | ||
} | ||
""" | ||
) | ||
|
||
|
||
class TwingateGroupAPIs: | ||
def get_group_id(self: TwingateClientProtocol, group_name: str) -> str | None: | ||
try: | ||
result = self.execute_gql( | ||
QUERY_GET_GROUP_ID_BY_NAME, variable_values={"name": group_name} | ||
) | ||
return result["groups"]["edges"][0]["node"]["id"] | ||
except (TransportQueryError, IndexError, KeyError): | ||
logging.exception("Failed to get resource") | ||
return None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import logging | ||
|
||
from gql import gql | ||
from gql.transport.exceptions import TransportQueryError | ||
|
||
from app.api.protocol import TwingateClientProtocol | ||
|
||
QUERY_GET_SA_ID_BY_NAME = gql( | ||
""" | ||
query GetServiceAccountByName($name: String!) { | ||
serviceAccounts(filter: {name: {eq: $name}}) { | ||
edges { | ||
node { | ||
id | ||
name | ||
} | ||
} | ||
} | ||
} | ||
""" | ||
) | ||
|
||
|
||
class TwingateServiceAccountAPIs: | ||
def get_service_account_id( | ||
self: TwingateClientProtocol, service_account_name: str | ||
) -> str | None: | ||
try: | ||
result = self.execute_gql( | ||
QUERY_GET_SA_ID_BY_NAME, variable_values={"name": service_account_name} | ||
) | ||
return result["serviceAccounts"]["edges"][0]["node"]["id"] | ||
except (TransportQueryError, IndexError, KeyError): | ||
logging.exception("Failed to get resource") | ||
return None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import json | ||
|
||
import responses | ||
|
||
|
||
class TestTwingateGroupsAPIs: | ||
def test_get_group_id_success(self, test_url, api_client, mocked_responses): | ||
success_response = json.dumps( | ||
{ | ||
"data": { | ||
"groups": {"edges": [{"node": {"id": "test-id", "name": "test"}}]} | ||
} | ||
} | ||
) | ||
|
||
mocked_responses.post( | ||
test_url, | ||
status=200, | ||
body=success_response, | ||
match=[ | ||
responses.matchers.json_params_matcher( | ||
{"variables": {"name": "test"}}, strict_match=False | ||
) | ||
], | ||
) | ||
result = api_client.get_group_id("test") | ||
assert result == "test-id" | ||
|
||
def test_get_group_id_not_found_returns_none( | ||
self, test_url, api_client, mocked_responses | ||
): | ||
success_response = json.dumps({"data": {"groups": {"edges": []}}}) | ||
|
||
mocked_responses.post( | ||
test_url, | ||
status=200, | ||
body=success_response, | ||
match=[ | ||
responses.matchers.json_params_matcher( | ||
{"variables": {"name": "test"}}, strict_match=False | ||
) | ||
], | ||
) | ||
result = api_client.get_group_id("test") | ||
assert result is None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import json | ||
|
||
import responses | ||
|
||
|
||
class TestTwingateServiceAccountAPIs: | ||
def test_get_service_account_id_success( | ||
self, test_url, api_client, mocked_responses | ||
): | ||
success_response = json.dumps( | ||
{ | ||
"data": { | ||
"serviceAccounts": { | ||
"edges": [{"node": {"id": "test-id", "name": "test"}}] | ||
} | ||
} | ||
} | ||
) | ||
|
||
mocked_responses.post( | ||
test_url, | ||
status=200, | ||
body=success_response, | ||
match=[ | ||
responses.matchers.json_params_matcher( | ||
{"variables": {"name": "test"}}, strict_match=False | ||
) | ||
], | ||
) | ||
result = api_client.get_service_account_id("test") | ||
assert result == "test-id" | ||
|
||
def test_get_service_account_id_not_found_returns_none( | ||
self, test_url, api_client, mocked_responses | ||
): | ||
success_response = json.dumps({"data": {"serviceAccounts": {"edges": []}}}) | ||
|
||
mocked_responses.post( | ||
test_url, | ||
status=200, | ||
body=success_response, | ||
match=[ | ||
responses.matchers.json_params_matcher( | ||
{"variables": {"name": "test"}}, strict_match=False | ||
) | ||
], | ||
) | ||
result = api_client.get_service_account_id("test") | ||
assert result is None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.