Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Network connectivity: Add key management for EAP secured networks #2254

Merged
merged 2 commits into from
Sep 14, 2018

Conversation

sfoster1
Copy link
Member

@sfoster1 sfoster1 commented Sep 12, 2018

Some EAP secured networks (in fact most kinds) either require or allow the use of key files for various tasks, such as client identity authentication through private keys or server identity authentication through x.509 certificates. In some cases, these keys can be large; so, rather than just adding them into the /wifi/configure request, here are some new endpoints to manage wifi keys.

They will be stored on disk in the new subdirectory /data/user_storage/opentrons_data/network_keys, available in opentrons.util.environment.settings as WIFI_KEYS_DIR. To allow upload of identically named keys, the key files will be stored in directories named by a hash of the key file itself. Identically-hashed keys are not allowed.

The shape of the endpoints is:

200 OK
    { keys: [
         {
          uri: '/wifi/keys/some-hex-digest',
          id: 'some-hex-digest',
          name: 'keyfile.pem'
         },
         ...
       ]
    }
}
POST /wifi/keys multipart/form-data with key=<file>
201 Created
{
     uri: '/wifi/keys/some-hex-digest',
     id: 'some-hex-digest',
     name: 'keyfile.pem'
}

If the key already exists:

409 Conflict
{
 message: 'Key file already present'
}
DELETE /wifi/keys/{key-id}
200 OK
{message: 'Key file {key-id} deleted'}

In addition, add proper types to nmcli and wifi endpoints.

Closes #2253

The nmcli module is now fully typed
@codecov
Copy link

codecov bot commented Sep 12, 2018

Codecov Report

Merging #2254 into edge will increase coverage by 0.86%.
The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff             @@
##             edge    #2254      +/-   ##
==========================================
+ Coverage   29.67%   30.54%   +0.86%     
==========================================
  Files         497      500       +3     
  Lines        7962     8253     +291     
==========================================
+ Hits         2363     2521     +158     
- Misses       5599     5732     +133
Impacted Files Coverage Δ
app/src/http-api-client/health.js 95% <0%> (-5%) ⬇️
discovery-client/src/poller.js 92.85% <0%> (-3.58%) ⬇️
app/src/http-api-client/settings.js 100% <0%> (ø) ⬆️
app/src/components/UploadPanel/Upload.js 0% <0%> (ø) ⬆️
...l-designer/src/containers/ConnectedNewFileModal.js 0% <0%> (ø) ⬆️
...tocol-designer/src/containers/ConnectedTitleBar.js 0% <0%> (ø) ⬆️
protocol-designer/src/containers/ConnectedNav.js 0% <0%> (ø) ⬆️
app/src/components/DeckMap/ConnectedSlotItem.js 0% <0%> (ø) ⬆️
...src/components/ReviewDeckModal/LabwareComponent.js 0% <0%> (ø) ⬆️
...ocol-designer/src/containers/ConnectedMainPanel.js 0% <0%> (ø) ⬆️
... and 17 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update d2982e1...1f448b1. Read the comment docs.

@sfoster1 sfoster1 force-pushed the api_add-wifi-key-management branch from 1f448b1 to 55467b4 Compare September 13, 2018 14:20
Copy link
Contributor

@mcous mcous left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks really good! I have a couple change requests related to error responses for configure and key upload

hidden=hidden)
status = 201 if ok else 401
try:
checked_sec = sec_translation[security]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this will overwrite the ssid missing error, which I think is the more pressing error. Also, to nitpick try/except blocks make me (irrationally?) nervous (thank you Scala). Maybe we could do something like this instead?

# ...
security = body.get('security_type')
# ...
if ssid is None:
    # ...
elif security not in sec_translation:
    # ...
#...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer to avoid doing further work here if only because the next PR is going to be about eap configuration and will include a lot of overhauls of this endpoint - I can fold it in there.

""" Add a key file (for later use in EAP config) to the system.

```
POST /wifi/keys with {'key': <file>} -> 201 Created
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Docs nitpick: this kind of makes it seem like the request body should be JSON when it should actually be multipart form data

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, how would you like me to specify it?

return web.json_response({'message': "Must upload key file"},
status=400)
data = await request.post()
if 'key' not in data:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would data.get('key') + if keyfile is None: be preferable here?

key_hash = hasher.hexdigest()
if key_hash in os.listdir(keys_dir):
return web.json_response(
{'message': 'Key file already present'}, status=409)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if it might be more helpful to respond with a 200 OK? The user wanted to upload a key, and that key is on the system. Regardless of if we change the status here, could we put the uri, id, and name in the response body?

POST /wifi/keys with {'key': <file>} -> 201 Created
{
uri: '/wifi/keys/some-hex-digest',
hash: 'some-hex-digest',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hash changed to id in actual response

""" Remove a key.

```
DELETE /wifi/keys/hex-digest
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DELETE /wifi/keys/:id might make it a little easier to understand that the last part of the URL is a parameter

@sfoster1 sfoster1 force-pushed the api_add-wifi-key-management branch 3 times, most recently from 02427b9 to f51b91d Compare September 13, 2018 18:59
/wifi/keys allows GET to list and POST to add a new keyfile (of arbitrary type and size). These are
hashed and stored in /data/user_storage/opentrons_data/network_keys in individual folders by sha256
hash of the contents. This hash is used as a URI for later deletion of the keys, or specification in
/wifi/configure eapol options that require key files.

Closes #2253
@sfoster1 sfoster1 force-pushed the api_add-wifi-key-management branch from f51b91d to 7e479de Compare September 13, 2018 19:01
Copy link
Contributor

@mcous mcous left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🍵

If a file with the same hash (regardless of filename) has already been
uploaded, returns
```
200 OK
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@@ -166,6 +166,9 @@ def init(loop=None):
'/wifi/configure', wifi.configure)
server.app.router.add_get(
'/wifi/status', wifi.status)
server.app.router.add_post('/wifi/keys', wifi.add_key)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note to self about app state: the paths and responses of the /wifi/keys endpoints are completely correct (in my mind), but to date the app hasn't really had to deal with ID'd resources. This functionality (along with module firmware update by serial number) will mean we need to make a very considered refactor of the app's state w.r.t. API resources

@mcous mcous added api Affects the `api` project ready for review feature Ticket is a feature request / PR introduces a feature labels Sep 14, 2018
Copy link
Contributor

@btmorr btmorr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🍐

@sfoster1 sfoster1 merged commit 250101c into edge Sep 14, 2018
@sfoster1 sfoster1 deleted the api_add-wifi-key-management branch September 14, 2018 14:36
mcous added a commit that referenced this pull request Sep 14, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api Affects the `api` project feature Ticket is a feature request / PR introduces a feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants