-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add config/map/token endpoint to support Data Catalog (#187)
* Add map token endpoint to tiler service The tiler service will generate a token for use against an azure maps instance, using the identity of the tiler (when deployed) or the local developer credentials (in local development). A test has been added that requires a local identity, and this has been skipped in CI, which does not have access to those kind of credentials. This endpoint will be used by the Data Catalog app to avoid distributing an azure maps key within that application. * Remove unneeded role assignment * Remove unused variables
- Loading branch information
1 parent
dcfd7c7
commit 1282fbb
Showing
28 changed files
with
290 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,15 @@ | ||
FROM mcr.microsoft.com/azure-cli:cbl-mariner2.0 | ||
|
||
# URL used to download the packages from the CFS | ||
ARG INDEX_URL | ||
ENV PIP_INDEX_URL=$INDEX_URL | ||
|
||
# Setup pip and server dependencies | ||
RUN python3 -m ensurepip --upgrade | ||
RUN pip3 install fastapi uvicorn[standard] azure-identity | ||
|
||
WORKDIR /opt/src | ||
|
||
COPY . /opt/src | ||
|
||
CMD uvicorn main:app --host 0.0.0.0 --port 8086 --reload --log-level info |
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 time | ||
from typing import Any, Optional | ||
from typing import Dict | ||
|
||
from azure.core.credentials import AccessToken | ||
from azure.identity import AzureCliCredential | ||
from fastapi import FastAPI | ||
|
||
app = FastAPI() | ||
|
||
|
||
class TokenProvider: | ||
_instance: Optional["TokenProvider"] = None | ||
|
||
_tokens: Dict[str, Optional[AccessToken]] = {} | ||
|
||
def __init__(self) -> None: | ||
self._token = None | ||
|
||
def get_token(self, resource: str) -> AccessToken: | ||
token = self._tokens.get(resource) | ||
if token is None or token.expires_on < time.time() - 5: | ||
token = AzureCliCredential().get_token(resource) | ||
self._tokens[resource] = token | ||
assert token is not None # neede for mypy | ||
return token | ||
|
||
@classmethod | ||
def get_instance(cls) -> "TokenProvider": | ||
if cls._instance is None: | ||
cls._instance = cls() | ||
return cls._instance | ||
|
||
|
||
@app.get("/dev/token") | ||
async def cli_token(resource: str = "") -> Dict[str, Any]: | ||
"""Uses the az cli credential to get a token for the given resource. This is | ||
meant to mimic the behavior of using managed identities in other spatio | ||
services in the development environment.""" | ||
accessToken = TokenProvider.get_instance().get_token(resource) | ||
return { | ||
"access_token": accessToken.token, | ||
"expires_on": accessToken.expires_on, | ||
"resource": resource, | ||
} |
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
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
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
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,5 @@ | ||
resource "azurerm_maps_account" "azmaps" { | ||
name = "azmaps-${local.prefix}" | ||
resource_group_name = azurerm_resource_group.pc.name | ||
sku_name = "G2" | ||
} |
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 |
---|---|---|
|
@@ -5,4 +5,4 @@ resource "azurerm_resource_group" "pc" { | |
tags = { | ||
"ringValue" = "r0" | ||
} | ||
} | ||
} |
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
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
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,28 @@ | ||
import threading | ||
from typing import Any | ||
|
||
from azure.core.credentials import AccessToken | ||
from azure.identity import DefaultAzureCredential | ||
|
||
|
||
class PcDefaultAzureCredential: | ||
"""Singleton wrapper around DefaultAzureCredential to share in memory cache | ||
between requests and threads. Assumption of thread safety for method calls is | ||
based on: | ||
https://github.com/Azure/azure-sdk-for-python/issues/28665 | ||
""" | ||
|
||
_instance = None | ||
_lock = threading.Lock() | ||
|
||
@classmethod | ||
def get_token(cls, *scopes: str, **kwargs: Any) -> AccessToken: | ||
return cls.get_credential().get_token(*scopes, **kwargs) | ||
|
||
@classmethod | ||
def get_credential(cls) -> DefaultAzureCredential: | ||
if cls._instance is None: | ||
with cls._lock: | ||
cls._instance = DefaultAzureCredential() | ||
return cls._instance |
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.