Skip to content

Commit

Permalink
refactor: identify if credentials are from gsa
Browse files Browse the repository at this point in the history
  • Loading branch information
dfguerrerom committed Apr 25, 2024
1 parent aa693e2 commit 3331310
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
13 changes: 10 additions & 3 deletions sepal_ui/scripts/decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ def init_ee() -> None:

# Extract the project name from credentials
_credentials = json.loads(credential_file_path.read_text())
project_id = os.environ.get(
"EARTHENGINE_PROJECT",
_credentials.get("project_id", _credentials.get("project", None)),
project_id = os.environ.get("EARTHENGINE_PROJECT", None)
project_id = project_id or _credentials.get(
"project_id", _credentials.get("project", None)
)

if not project_id:
Expand All @@ -71,6 +71,13 @@ def init_ee() -> None:
"Please set the EARTHENGINE_PROJECT environment variable. "
"Or authenticate using `earthengine set_project project_name`."
)
# Check if we are using a google service account
if _credentials.get("type") == "service_account":
ee_user = _credentials.get("client_email")
credentials = ee.ServiceAccountCredentials(
ee_user, str(credential_file_path)
)
ee.Initialize(credentials=credentials, http_transport=httplib2.Http())

# if the user is in local development the authentication should
# already be available
Expand Down
14 changes: 10 additions & 4 deletions sepal_ui/scripts/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,9 @@ def init_ee() -> None:

# Extract the project name from credentials
_credentials = json.loads(credential_file_path.read_text())
project_id = os.environ.get(
"EARTHENGINE_PROJECT",
_credentials.get("project_id", _credentials.get("project", None)),
project_id = os.environ.get("EARTHENGINE_PROJECT", None)
project_id = project_id or _credentials.get(
"project_id", _credentials.get("project", None)
)

if not project_id:
Expand All @@ -164,7 +164,13 @@ def init_ee() -> None:
"Please set the EARTHENGINE_PROJECT environment variable. "
"Or authenticate using `earthengine set_project project_name`."
)

# Check if we are using a google service account
if _credentials.get("type") == "service_account":
ee_user = _credentials.get("client_email")
credentials = ee.ServiceAccountCredentials(
ee_user, str(credential_file_path)
)
ee.Initialize(credentials=credentials, http_transport=httplib2.Http())
# if the user is in local development the authentication should
# already be available
ee.Initialize(project=project_id, http_transport=httplib2.Http())
Expand Down

0 comments on commit 3331310

Please sign in to comment.