-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpendinghost.go
159 lines (126 loc) · 5.01 KB
/
pendinghost.go
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// Copyright 2024 qbee.io
//
// 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.
//
// SPDX-License-Identifier: Apache-2.0
package client
import (
"context"
"fmt"
"net/http"
)
// PendingDevice represents a device that is pending approval.
type PendingDevice struct {
// PublicKeyDigest - device's public key digest (used to identify devices).
PublicKeyDigest string `json:"pub_key_digest"`
// Approved is true when the device is approved.
Approved bool `json:"approved"`
// ApprovedTimestamp in UnixNano.
ApprovedTimestamp int64 `json:"approved_timestamp,omitempty"`
// Certificate - base64-encoded (PEM) signed device certificate.
Certificate string `json:"cert,omitempty"`
// GroupID where device is assigned.
GroupID string `json:"group_id,omitempty"`
// RemoteAddress from which device was bootstrapped.
RemoteAddress string `json:"remoteaddress"`
// Device creation timestamp (in seconds).
Timestamp int64 `json:"timestamp"`
// Host - The name of the current host, according to the kernel.
// It is undefined whether this is qualified or unqualified with a domain name.
Host string `json:"host"`
// FQHost - The fully qualified name of the host (e.g. "device1.example.com").
FQHost string `json:"fqhost"`
// UQHost - The unqualified name of the host (e.g. "device1").
UQHost string `json:"uqhost"`
// DeviceName - The custom name for the device.
DeviceName string `json:"device_name,omitempty"`
// HardwareMAC - This contains the MAC address of the named interface map[interface]macAddress.
// Note: The keys in this array are canonified.
// For example, the entry for wlan0.1 would be found under the wlan0_1 key.
//
// Example:
// {
// "ens1": "52:54:00:4a:db:ee",
// "qbee0": "00:00:00:00:00:00"
// }
HardwareMAC map[string]string `json:"hardware_mac"`
// IPDefault - All four octets of the IPv4 address of the first system interface.
//Note: If the system has a single ethernet interface, this variable will contain the IPv4 address.
// However, if the system has multiple interfaces, then this variable will simply be the IPv4 address of the first
// interface in the list that has an assigned address.
// Use IPv4[interface_name] for details on obtaining the IPv4 addresses of all interfaces on a system.
IPDefault string `json:"ip_default"`
// IPv4 - All IPv4 addresses of the system mapped by interface name.
// Example:
// {
// "ens1": "192.168.122.239",
// "qbee0": "100.64.39.78"
// }
IPv4 map[string]string `json:"ipv4"`
// RawPublicKey of the device as slice of PEM-encoded lines.
// Example:
// []string{
// "-----BEGIN PUBLIC KEY-----",
// "MIGbMBAGByqGSM49AgEGBSuBBAAjA4GGAAQBvDALiaU+dyvd1DhMUCEXnuX4h5r5",
// "ikBVNSl88QBtBoxtQy1XsCJ7Dm/tzoQ1YPYT80oVTdExK/oFnZFvI89SX8sBN89L",
// "Y8q+8BBQPLf1nA3DG7apq7xq11Zkpde2eK0pCUG7nZPisXlU96C44NLE62TzDYEZ",
// "RYkhJQhFeNOlFSpF/xA=",
// "-----END PUBLIC KEY-----"
// }
RawPublicKey []string `json:"pub_key,omitempty"`
}
const pendingHostPath = "/api/v2/pendinghost"
// GetPendingDevices returns the pending devices list.
func (cli *Client) GetPendingDevices(ctx context.Context, pendingOnly bool) ([]PendingDevice, error) {
var devices []PendingDevice
path := pendingHostPath
if pendingOnly {
path += "?pendingonly=1"
}
if err := cli.Call(ctx, http.MethodGet, path, nil, &devices); err != nil {
return nil, err
}
return devices, nil
}
// ApprovePendingDeviceRequest represents a request to approve a pending device.
type ApprovePendingDeviceRequest struct {
// NodeID is the node ID of the device to approve.
NodeID string `json:"node_id"`
}
// ApprovePendingDevice approves a pending device with provided node ID.
func (cli *Client) ApprovePendingDevice(ctx context.Context, nodeID string) error {
data := ApprovePendingDeviceRequest{
NodeID: nodeID,
}
if err := cli.Call(ctx, http.MethodPost, pendingHostPath, data, nil); err != nil {
return err
}
return nil
}
// RejectPendingDevice rejects a pending device with provided node ID.
func (cli *Client) RejectPendingDevice(ctx context.Context, nodeID string) error {
path := pendingHostPath + "/" + nodeID
if err := cli.Call(ctx, http.MethodDelete, path, nil, nil); err != nil {
return err
}
return nil
}
const removeApprovedHostPath = "/api/v2/removeapprovedhost/%s"
// RemoveApprovedHost rejects a pending device with provided node ID.
func (cli *Client) RemoveApprovedHost(ctx context.Context, nodeID string) error {
path := fmt.Sprintf(removeApprovedHostPath, nodeID)
if err := cli.Call(ctx, http.MethodDelete, path, nil, nil); err != nil {
return err
}
return nil
}