|
| 1 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +# or more contributor license agreements. See the NOTICE file |
| 3 | +# distributed with this work for additional information |
| 4 | +# regarding copyright ownership. The ASF licenses this file |
| 5 | +# to you under the Apache License, Version 2.0 (the |
| 6 | +# "License"); you may not use this file except in compliance |
| 7 | +# with the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, |
| 12 | +# software distributed under the License is distributed on an |
| 13 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +# KIND, either express or implied. See the License for the |
| 15 | +# specific language governing permissions and limitations |
| 16 | +# under the License. |
| 17 | + |
| 18 | +from __future__ import annotations |
| 19 | + |
| 20 | +import logging |
| 21 | + |
| 22 | +from fastapi import Depends, HTTPException, status |
| 23 | +from typing_extensions import Annotated |
| 24 | + |
| 25 | +from airflow.api_fastapi.common.router import AirflowRouter |
| 26 | +from airflow.api_fastapi.execution_api import datamodels |
| 27 | +from airflow.models.variable import Variable |
| 28 | + |
| 29 | +# TODO: Add dependency on JWT token |
| 30 | +router = AirflowRouter( |
| 31 | + responses={status.HTTP_404_NOT_FOUND: {"description": "Variable not found"}}, |
| 32 | +) |
| 33 | + |
| 34 | +log = logging.getLogger(__name__) |
| 35 | + |
| 36 | + |
| 37 | +def get_task_token() -> datamodels.TIToken: |
| 38 | + """TODO: Placeholder for task identity authentication. This should be replaced with actual JWT decoding and validation.""" |
| 39 | + return datamodels.TIToken(ti_key="test_key") |
| 40 | + |
| 41 | + |
| 42 | +@router.get( |
| 43 | + "/{variable_key}", |
| 44 | + responses={ |
| 45 | + status.HTTP_401_UNAUTHORIZED: {"description": "Unauthorized"}, |
| 46 | + status.HTTP_403_FORBIDDEN: {"description": "Task does not have access to the variable"}, |
| 47 | + }, |
| 48 | +) |
| 49 | +def get_variable( |
| 50 | + variable_key: str, |
| 51 | + token: Annotated[datamodels.TIToken, Depends(get_task_token)], |
| 52 | +) -> datamodels.VariableResponse: |
| 53 | + """Get an Airflow Variable.""" |
| 54 | + if not has_variable_access(variable_key, token): |
| 55 | + raise HTTPException( |
| 56 | + status_code=status.HTTP_403_FORBIDDEN, |
| 57 | + detail={ |
| 58 | + "reason": "access_denied", |
| 59 | + "message": f"Task does not have access to variable {variable_key}", |
| 60 | + }, |
| 61 | + ) |
| 62 | + |
| 63 | + try: |
| 64 | + variable_value = Variable.get(variable_key) |
| 65 | + except KeyError: |
| 66 | + raise HTTPException( |
| 67 | + status.HTTP_404_NOT_FOUND, |
| 68 | + detail={ |
| 69 | + "reason": "not_found", |
| 70 | + "message": f"Variable with key '{variable_key}' not found", |
| 71 | + }, |
| 72 | + ) |
| 73 | + |
| 74 | + return datamodels.VariableResponse(key=variable_key, value=variable_value) |
| 75 | + |
| 76 | + |
| 77 | +def has_variable_access(variable_key: str, token: datamodels.TIToken) -> bool: |
| 78 | + """Check if the task has access to the variable.""" |
| 79 | + # TODO: Placeholder for actual implementation |
| 80 | + |
| 81 | + ti_key = token.ti_key |
| 82 | + log.debug( |
| 83 | + "Checking access for task instance with key '%s' to variable '%s'", |
| 84 | + ti_key, |
| 85 | + variable_key, |
| 86 | + ) |
| 87 | + return True |
0 commit comments