Skip to content

Commit

Permalink
black with line-length 120
Browse files Browse the repository at this point in the history
  • Loading branch information
sametd committed Apr 11, 2024
1 parent f2737fd commit 3467d7d
Show file tree
Hide file tree
Showing 14 changed files with 31 additions and 40 deletions.
1 change: 0 additions & 1 deletion polytope_server/common/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@


class AuthHelper:

"""A helper to encapsulate checking user authentication and authorization"""

def __init__(self, config):
Expand Down
2 changes: 1 addition & 1 deletion polytope_server/common/authentication/authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def name(self) -> str:
"plain": "PlainAuthentication",
"keycloak": "KeycloakAuthentication",
"federation": "FederationAuthentication",
"jwt" : "JWTAuthentication",
"jwt": "JWTAuthentication",
}


Expand Down
6 changes: 2 additions & 4 deletions polytope_server/common/authentication/jwt_authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,8 @@ def authenticate(self, credentials: str) -> User:

try:
certs = self.get_certs()
decoded_token = jwt.decode(token=credentials,
algorithms=jwt.get_unverified_header(credentials).get('alg'),
key=certs
decoded_token = jwt.decode(
token=credentials, algorithms=jwt.get_unverified_header(credentials).get("alg"), key=certs
)

user = User(decoded_token["sub"], self.realm())
Expand All @@ -67,6 +66,5 @@ def authenticate(self, credentials: str) -> User:
raise ForbiddenRequest("Credentials could not be unpacked")
return user


def collect_metric_info(self):
return {}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@


class ApiKeyMongoAuthentication(authentication.Authentication):

"""
Authenticates a user using a polytope API key. A polytope API key is an alias to a user that was previously
authenticated. It allows user to authenticate once, retrieve a key, and use that for future authentication.
Expand Down
28 changes: 16 additions & 12 deletions polytope_server/common/authorization/ldap_authorization.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ def get_roles(self, user: User) -> list:
)
try:
if self.username_attribute is None:
return retrieve_ldap_user_roles(user.username, self.filter, self.url, self.search_base, self.ldap_user, self.ldap_password)
return retrieve_ldap_user_roles(
user.username, self.filter, self.url, self.search_base, self.ldap_user, self.ldap_password
)
else:
return retrieve_ldap_user_roles(
user.attributes[self.username_attribute],
Expand All @@ -69,10 +71,12 @@ def collect_metric_info(self):
#################################################


def retrieve_ldap_user_roles(uid: str, filter: str, url: str, search_base: str, ldap_user: str, ldap_password: str) -> list:
def retrieve_ldap_user_roles(
uid: str, filter: str, url: str, search_base: str, ldap_user: str, ldap_password: str
) -> list:
"""
Takes an ECMWF UID and returns all roles matching
the provided filter 'filter'.
Takes an ECMWF UID and returns all roles matching
the provided filter 'filter'.
"""

server = Server(url)
Expand All @@ -81,20 +85,20 @@ def retrieve_ldap_user_roles(uid: str, filter: str, url: str, search_base: str,
server,
user="CN={},OU=Connectors,OU=Service Accounts,DC=ecmwf,DC=int".format(ldap_user),
password=ldap_password,
raise_exceptions=True
raise_exceptions=True,
)

with connection as conn:
conn.search(
search_base=search_base,
search_filter='(&(objectClass=person)(cn={}))'.format(uid),
search_filter="(&(objectClass=person)(cn={}))".format(uid),
search_scope=SUBTREE,
attributes=['memberOf']
attributes=["memberOf"],
)
user_data = json.loads(conn.response_to_json())
if len(user_data['entries']) == 0:
raise KeyError('User {} not found in LDAP.'.format(uid))
roles = user_data['entries'][0]['attributes']['memberOf']
if len(user_data["entries"]) == 0:
raise KeyError("User {} not found in LDAP.".format(uid))
roles = user_data["entries"][0]["attributes"]["memberOf"]

# Filter roles
matches = []
Expand All @@ -104,7 +108,7 @@ def retrieve_ldap_user_roles(uid: str, filter: str, url: str, search_base: str,

# Parse CN=x,OU=y,OU=z,... into dict and extract 'common name' (CN)
for i, role in enumerate(matches):
d = dict(s.split('=') for s in role.split(','))
matches[i] = d['CN']
d = dict(s.split("=") for s in role.split(","))
matches[i] = d["CN"]

return matches
9 changes: 4 additions & 5 deletions polytope_server/common/datasource/mars.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,18 +84,17 @@ def match(self, request):
r = yaml.safe_load(request.user_request) or {}

for k, v in self.match_rules.items():

# An empty match rule means that the key must not be present
if v is None or len(v) == 0:
if k in r:
raise Exception("Request containing key '{}' is not allowed".format(k))
else:
continue # no more checks to do
continue # no more checks to do

# Check that all required keys exist
if k not in r and not (v is None or len(v) == 0):
raise Exception("Request does not contain expected key '{}'".format(k))


# Process date rules
if k == "date":
Expand Down Expand Up @@ -196,7 +195,7 @@ def make_env(self, request):
"MARS_USER_EMAIL": mars_user,
"MARS_USER_TOKEN": mars_token,
"ECMWF_MARS_COMMAND": self.mars_binary,
"FDB5_CONFIG" : yaml.dump(self.fdb_config[0])
"FDB5_CONFIG": yaml.dump(self.fdb_config[0]),
}

if self.mars_config is not None:
Expand Down
14 changes: 6 additions & 8 deletions polytope_server/common/datasource/polytope.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,19 +71,16 @@ def __init__(self, config):

# Set up polytope feature extraction library
self.polytope_options = {
"values": {
"mapper": {"type": "octahedral", "resolution": 1280, "axes": ["latitude", "longitude"]}
},
"values": {"mapper": {"type": "octahedral", "resolution": 1280, "axes": ["latitude", "longitude"]}},
"date": {"merge": {"with": "time", "linkers": ["T", "00"]}},
"step": {"type_change": "int"},
"number": {"type_change": "int"},
"longitude" : {"cyclic": [0, 360]},
"longitude": {"cyclic": [0, 360]},
}

logging.info("Set up gribjump")


#todo: remove when we no longer need to set up a valid fdb to use gribjump
# todo: remove when we no longer need to set up a valid fdb to use gribjump
def check_schema(self):

schema = self.fdb_config.get("schema", None)
Expand Down Expand Up @@ -131,7 +128,7 @@ def git_download_schema(self, remote, branch, git_dir, git_file):
logging.debug("Fetching FDB schema from git with call: {}".format(call))
output = subprocess.check_output(call, shell=True)
return output.decode("utf-8")

def get_type(self):
return self.type

Expand All @@ -152,6 +149,7 @@ def retrieve(self, request):
logging.info(self.polytope_config)
logging.info(self.polytope_options)
from polytope_mars.api import PolytopeMars

p = PolytopeMars(self.polytope_config, self.polytope_options)

self.output = p.extract(r)
Expand All @@ -177,7 +175,7 @@ def match(self, request):

if r[k] not in v:
raise Exception("got {} : {}, but expected one of {}".format(k, r[k], v))

# Finally check that there is a feature specified in the request
if "feature" not in r:
raise Exception("Request does not contain expected key 'feature'")
Expand Down
1 change: 0 additions & 1 deletion polytope_server/common/io/fifo.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@


class FIFO:

"""Creates a named pipe (FIFO) and reads data from it"""

def __init__(self, name, dir=None):
Expand Down
1 change: 0 additions & 1 deletion polytope_server/common/metric.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ class MetricType(enum.Enum):


class Metric:

"""A sealed class representing a metric"""

__slots__ = ["uuid", "timestamp", "type"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@


class MetricCollector:

"""Interface to collect metrics from any component in Polytope
for which a collector is implemented.
"""
Expand Down
1 change: 0 additions & 1 deletion polytope_server/common/metric_store/metric_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@


class MetricStore(ABC):

"""MetricStore is an interface for database-based storage for Metric objects"""

def __init__(self):
Expand Down
1 change: 0 additions & 1 deletion polytope_server/common/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ class Verb(enum.Enum):


class Request:

"""A sealed class representing a request"""

__slots__ = [
Expand Down
1 change: 0 additions & 1 deletion polytope_server/common/request_store/request_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@


class RequestStore(ABC):

"""RequestStore is an interface for database-based storage for Request objects"""

def __init__(self):
Expand Down
4 changes: 2 additions & 2 deletions polytope_server/frontend/flask_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,15 +218,15 @@ def uploads(request_id):
@handler.route("/api/v1/collections", methods=["GET"])
def list_collections():
auth_header = get_auth_header(request)
collection_config = authorized_collections = []
collection_config = authorized_collections = []
for name, collection in collections.items():
try:
if auth.can_access_collection(auth_header, collection):
authorized_collections.append(name)
except ForbiddenRequest:
pass
return RequestSucceeded(authorized_collections)

# New handler
# @handler.route("/api/v1/collection/<collection>", methods=["GET"])
# def describe_collection(collection):
Expand Down

0 comments on commit 3467d7d

Please sign in to comment.