File: api.py

package info (click to toggle)
mender-cli 1.9.0%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 428 kB
  • sloc: python: 534; makefile: 107; sh: 6
file content (82 lines) | stat: -rw-r--r-- 2,643 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/python
# Copyright 2021 Northern.tech AS
#
#    Licensed 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.
import os
import json

import requests

import crypto


def make_api_url(url, path):
    return os.path.join(url, path if not path.startswith("/") else path[1:])


class Deployments:
    def __init__(self, token, url='https://mender-api-gateway/api/management/v1/deployments'):
        self.url=url
        self.token=token

    def get_artifacts(self):
        auth = {'Authorization': 'Bearer {}'.format(self.token)}
        return requests.get(make_api_url(self.url, "/artifacts"), verify=False, headers=auth)


class DevicesDevauth:
    def __init__(self, url="https://mender-api-gateway/api/devices/v1/authentication"):
        self.url = url

    def auth_req(self, id_data, pubkey, privkey):
        payload = {
            "id_data": json.dumps(id_data),
            "pubkey": pubkey,
        }
        signature = crypto.auth_req_sign_rsa(json.dumps(payload), privkey)
        return payload, {"X-MEN-Signature": signature}

    def post_auth_request(self, authset, pubkey, privkey):
        body, sighdr = self.auth_req(authset, pubkey, privkey)
        return requests.post(
            make_api_url(self.url, "/auth_requests"),
            verify=False,
            headers=sighdr,
            json=body,
        )


class ManagementDevauth:
    def __init__(
        self, token, url="https://mender-api-gateway/api/management/v2/devauth"
    ):
        self.url = url
        self.token = token

    def get_devices(self):
        auth = {"Authorization": "Bearer {}".format(self.token)}
        return requests.get(
            make_api_url(self.url, "/devices"), verify=False, headers=auth
        )

    def set_device_auth_status(self, device_id, authset_id, auth_status):
        body = {
            "status": auth_status,
        }
        auth = {"Authorization": "Bearer {}".format(self.token)}
        return requests.put(
            make_api_url(self.url, f"/devices/{device_id}/auth/{authset_id}/status"),
            verify=False,
            headers=auth,
            json=body,
        )