Skip to content

Commit

Permalink
[#1592] Update API of ifm adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
tenthe committed Jun 5, 2023
2 parents 2ceb7ac + 752289c commit 296d967
Show file tree
Hide file tree
Showing 11 changed files with 184 additions and 89 deletions.
27 changes: 2 additions & 25 deletions VULNERABILITY.md
Original file line number Diff line number Diff line change
@@ -1,32 +1,9 @@
<!--
~ Licensed to the Apache Software Foundation (ASF) under one or more
~ contributor license agreements. See the NOTICE file distributed with
~ this work for additional information regarding copyright ownership.
~ The ASF licenses this file to You under the Apache License, Version 2.0
~ (the "License"); you may not use this file except in compliance with
~ the License. You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
~
-->

<!--\n ~ Licensed to the Apache Software Foundation (ASF) under one or more\n ~ contributor license agreements. See the NOTICE file distributed with\n ~ this work for additional information regarding copyright ownership.\n ~ The ASF licenses this file to You under the Apache License, Version 2.0\n ~ (the "License"); you may not use this file except in compliance with\n ~ the License. You may obtain a copy of the License at\n ~\n ~ http://www.apache.org/licenses/LICENSE-2.0\n ~\n ~ Unless required by applicable law or agreed to in writing, software\n ~ distributed under the License is distributed on an "AS IS" BASIS,\n ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n ~ See the License for the specific language governing permissions and\n ~ limitations under the License.\n ~\n -->
| OSV URL | Ecosystem | Package | Version | Source |
| --- | --- | --- | --- | --- |
| https://osv.dev/GHSA-599f-7c49-w659 | Maven | org.apache.commons:commons-text | 1.8 | pom.xml |
| https://osv.dev/GHSA-wxqc-pxw9-g2p8 | Maven | org.springframework:spring-core | 6.0.7 | pom.xml |
| https://osv.dev/GHSA-mjmj-j48q-9wg2 | Maven | org.yaml:snakeyaml | 1.33 | pom.xml |
| https://osv.dev/GHSA-px8h-6qxv-m22q | PyPI | werkzeug | 1.0.1 | streampipes-wrapper-python/requirements.txt |
| https://osv.dev/GHSA-xg9f-g7g7-2323 | PyPI | werkzeug | 1.0.1 | streampipes-wrapper-python/requirements.txt |
| https://osv.dev/PYSEC-2022-203 | PyPI | werkzeug | 1.0.1 | streampipes-wrapper-python/requirements.txt |
| https://osv.dev/GHSA-36jr-mh4h-2g58 | npm | d3-color | 1.4.1 | ui/package-lock.json |
| https://osv.dev/GHSA-j8r2-6x86-q33q | PyPI | requests | 2.24.0 | streampipes-wrapper-python/requirements.txt |
| https://osv.dev/GHSA-36jr-mh4h-2g58 | npm | d3-color | 2.0.0 | ui/package-lock.json |
| https://osv.dev/GHSA-ww39-953v-wcq6 | npm | glob-parent | 3.1.0 | ui/package-lock.json |
| https://osv.dev/GHSA-4943-9vgg-gr5r | npm | quill | 1.3.7 | ui/package-lock.json |
| https://osv.dev/GHSA-hc6q-2mpp-qw7j | npm | webpack | 5.74.0 | ui/package-lock.json |
| https://osv.dev/GHSA-f9xv-q969-pqx4 | npm | yaml | 2.2.1 | ui/package-lock.json |
5 changes: 3 additions & 2 deletions streampipes-client-python/docs/getting-started/quickstart.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,11 @@ Alternatively, you can provide your credentials via environment variables.
Simply define your credential provider as follows:

```python
>>> from streampipes.client.credential_provider import StreamPipesApiKeyCredentials
from streampipes.client.credential_provider import StreamPipesApiKeyCredentials

StreamPipesApiKeyCredentials.from_env(username_env="USER", api_key_env="API-KEY")
StreamPipesApiKeyCredentials()
```
This requires to set the following environment variables: `SP_API_KEY` and `SP_USERNAME`
<br>

`username` is always the username that is used to log in into StreamPipes. <br>
Expand Down
3 changes: 2 additions & 1 deletion streampipes-client-python/docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,9 @@ Simply define your credential provider as follows:
```python
from streampipes.client.credential_provider import StreamPipesApiKeyCredentials

StreamPipesApiKeyCredentials.from_env(username_env="USER", api_key_env="API-KEY")
StreamPipesApiKeyCredentials()
```
This requires to set the following environment variables: `SP_API_KEY` and `SP_USERNAME`
<br>

`username` is always the username that is used to log in into StreamPipes. <br>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@
"execution_count": null,
"outputs": [],
"source": [
"StreamPipesApiKeyCredentials.from_env(username_env=\"USER\", api_key_env=\"API-KEY\")"
"StreamPipesApiKeyCredentials()"
],
"metadata": {
"collapsed": false
Expand All @@ -158,9 +158,8 @@
{
"cell_type": "markdown",
"source": [
"Please note that you pass the names of the environment variables.\n",
"To ensure that the above code works, you must set the environment variables with the same name you specified in `from_env`.\n",
"In this scenario this would look like the following:"
"To ensure that the above code works, you must set the environment variables as expected.\n",
"This can be done like following:"
],
"metadata": {
"collapsed": false
Expand All @@ -171,8 +170,8 @@
"execution_count": null,
"outputs": [],
"source": [
"%export USER=\"<USERNAME>\"\n",
"%export API_KEY=\"<API-KEY>\""
"%export SP_USERNAME=\"<USERNAME>\"\n",
"%export SP_API_KEY=\"<API-KEY>\""
],
"metadata": {
"collapsed": false
Expand Down Expand Up @@ -256,13 +255,6 @@
"metadata": {
"collapsed": false
}
},
{
"cell_type": "markdown",
"source": [],
"metadata": {
"collapsed": false
}
}
],
"metadata": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@
"outputs": [],
"source": [
"import os\n",
"os.environ[\"USER\"] = \"[email protected]\"\n",
"os.environ[\"API-KEY\"] = \"XXX\"\n"
"os.environ[\"SP_USERNAME\"] = \"[email protected]\"\n",
"os.environ[\"SP_API_KEY\"] = \"XXX\"\n"
],
"metadata": {
"collapsed": false
Expand All @@ -59,7 +59,7 @@
"outputs": [],
"source": [
"config = StreamPipesClientConfig(\n",
" credential_provider=StreamPipesApiKeyCredentials.from_env(username_env=\"USER\", api_key_env=\"API-KEY\"),\n",
" credential_provider=StreamPipesApiKeyCredentials(),\n",
" host_address=\"localhost\",\n",
" https_disabled=True,\n",
" port=80\n",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@
"source": [
"import os\n",
"\n",
"os.environ[\"USER\"] = \"[email protected]\"\n",
"os.environ[\"API-KEY\"] = \"XXX\"\n",
"os.environ[\"SP_USERNAME\"] = \"[email protected]\"\n",
"os.environ[\"SP_API_KEY\"] = \"XXX\"\n",
"\n",
"# Use this if you work locally:\n",
"os.environ[\"BROKER-HOST\"] = \"localhost\" \n",
Expand All @@ -74,7 +74,7 @@
],
"source": [
"client_config = StreamPipesClientConfig(\n",
" credential_provider=StreamPipesApiKeyCredentials.from_env(username_env=\"USER\", api_key_env=\"API-KEY\"),\n",
" credential_provider=StreamPipesApiKeyCredentials(),\n",
" host_address=\"localhost\",\n",
" port=80,\n",
" https_disabled=True,\n",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@
"source": [
"import os\n",
"\n",
"os.environ[\"USER\"] = \"[email protected]\"\n",
"os.environ[\"API-KEY\"] = \"XXX\"\n",
"os.environ[\"SP_USERNAME\"] = \"[email protected]\"\n",
"os.environ[\"SP_API_KEY\"] = \"XXX\"\n",
"\n",
"# Use this if you work locally:\n",
"os.environ[\"BROKER-HOST\"] = \"localhost\" \n",
Expand All @@ -63,7 +63,7 @@
],
"source": [
"client_config = StreamPipesClientConfig(\n",
" credential_provider=StreamPipesApiKeyCredentials.from_env(username_env=\"USER\", api_key_env=\"API-KEY\"),\n",
" credential_provider=StreamPipesApiKeyCredentials(),\n",
" host_address=\"localhost\",\n",
" port=80,\n",
" https_disabled=True,\n",
Expand Down
3 changes: 2 additions & 1 deletion streampipes-client-python/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
"pydantic>=1.10.2",
"requests>=2.28.1",
"nats-py>=2.2.0",
"confluent-kafka>=2.0.2"
"confluent-kafka>=2.0.2",
"typing-extensions>=4.5.0"
]

dev_packages = base_packages + [
Expand Down
85 changes: 73 additions & 12 deletions streampipes-client-python/streampipes/client/credential_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
"StreamPipesApiKeyCredentials",
]

from typing_extensions import deprecated


class CredentialProvider(ABC):
"""Abstract implementation of a credential provider.
Expand Down Expand Up @@ -81,22 +83,36 @@ class StreamPipesApiKeyCredentials(CredentialProvider):
The required token can be generated via the StreamPipes UI (see the description on our [start-page](../../../).
Both parameters can either be passed as arguments or remain unset.
If they are not passed, they are retrieved from environment variables:
* `SP_USERNAME` is expected to contain the username
* `SP_API_KEY` is expected to contain the API key
Parameters
----------
username: str
The username to which the API token is granted, e.g., `[email protected]`.
api_key: str
The StreamPipes API key as it is displayed in the UI.
username: Optional[str]
The username to which the API token is granted, e.g., `[email protected]`.<br>
If not passed, the username is retrieved from environment variable `SP_USERNAME`.
api_key: Optional[str]
The StreamPipes API key as it is displayed in the UI.<br>
If not passed, the api key is retrieved from environment variable `SP_API_KEY`
Examples
--------
see [StreamPipesClient][streampipes.client.StreamPipesClient]
"""

_ENV_KEY_API = "SP_API_KEY"
_ENV_KEY_USERNAME = "SP_USERNAME"

@classmethod
@deprecated("deprecated since 0.93.0; please use the class constructor instead.")
def from_env(cls, username_env: str, api_key_env: str) -> StreamPipesApiKeyCredentials:
"""Returns an api key provider parameterized via environment variables.
"""DEPRECATED - use the class constructor instead
Returns an api key provider parameterized via environment variables.
Parameters
----------
Expand All @@ -123,19 +139,60 @@ def from_env(cls, username_env: str, api_key_env: str) -> StreamPipesApiKeyCrede
raise KeyError(
f"Ups, the following environment variables have not been found: "
f"{'`' + username_env + '`,' if username is None else ''}"
f"{'`' + api_key_env +'`' if api_key is None else ''}. " # noqa: W291
f"{'`' + api_key_env + '`' if api_key is None else ''}. " # noqa: W291
"Please check them to be properly set."
)

return cls(username=username, api_key=api_key)

def __init__(
self,
username: str,
api_key: str,
username: Optional[str] = None,
api_key: Optional[str] = None,
):
self.username = username
self.api_key = api_key
# if both parameters are passed we can add them directly to the instance
if all({username, api_key}):
self.username = username
self.api_key = api_key

# otherwise we need to check if environment variables are properly set
else:
retrieved_api_key = os.environ.get(self._ENV_KEY_API, None)
retrieved_user_name = os.environ.get(self._ENV_KEY_USERNAME, None)

self.username = retrieved_user_name
self.api_key = retrieved_api_key

if username:
self.username = username

if retrieved_api_key is None:
raise AttributeError(
"API key not found in the environment variables - please provide the API key "
"via the parameter `api_key` or ensure that it is passed to "
f"the environment variable `{self._ENV_KEY_API}`."
)
self.api_key = retrieved_api_key

if api_key:
self.api_key = api_key

if retrieved_user_name is None:
raise AttributeError(
"Username not found - please provide the username "
"via the parameter `username` or ensure that it is passed to "
f"the environment variable `{self._ENV_KEY_USERNAME}`."
)
self.username = retrieved_user_name

if not all({self.username, self.api_key}):
if None in {retrieved_user_name, retrieved_api_key}:
raise AttributeError(
"Both parameters not found - please provide both api key and username "
"either via the parameters (`api_key` and `username`) or via "
f"the corresponding environment variables (`{self._ENV_KEY_API}` and "
f"`{self._ENV_KEY_USERNAME}`)."
)

@property
def _authentication_headers(self) -> Dict[str, str]:
Expand All @@ -146,7 +203,11 @@ def _authentication_headers(self) -> Dict[str, str]:
Dictionary with authentication headers as string key-value pairs.
"""

user: str = self.username # type: ignore # mypy mistakenly detects this attribute as optional
token: str = self.api_key # type: ignore # mypy mistakenly detects this attribute as optional

return {
"X-API-User": self.username,
"X-API-Key": self.api_key,
"X-API-User": user,
"X-API-Key": token,
}
Loading

0 comments on commit 296d967

Please sign in to comment.