-
Notifications
You must be signed in to change notification settings - Fork 18
/
register.go
406 lines (348 loc) · 9.64 KB
/
register.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
package main
import (
"encoding/json"
"fmt"
"io"
"net/url"
"os"
"github.com/godbus/dbus/v5"
)
func getConsumerUUID() (string, error) {
conn, err := dbus.SystemBus()
if err != nil {
return "", err
}
locale := getLocale()
var uuid string
if err := conn.Object(
"com.redhat.RHSM1",
"/com/redhat/RHSM1/Consumer").Call(
"com.redhat.RHSM1.Consumer.GetUuid",
dbus.Flags(0),
locale).Store(&uuid); err != nil {
return "", unpackRHSMError(err)
}
return uuid, nil
}
// Organization is structure containing information about RHSM organization (sometimes called owner)
// JSON document returned from candlepin server can have the following format. We care only about key,
// but it can be extended and more information can be added to the structure in the future.
//
// {
// "created": "2022-11-02T16:00:23+0000",
// "updated": "2022-11-02T16:00:48+0000",
// "id": "4028face84391264018439127db10004",
// "displayName": "Donald Duck",
// "key": "donaldduck",
// "contentPrefix": null,
// "defaultServiceLevel": null,
// "logLevel": null,
// "contentAccessMode": "org_environment",
// "contentAccessModeList": "entitlement,org_environment",
// "autobindHypervisorDisabled": false,
// "autobindDisabled": false,
// "lastRefreshed": "2022-11-02T16:00:48+0000",
// "parentOwner": null,
// "upstreamConsumer": null
// }
type Organization struct {
Key string `json:"key"`
}
// unpackOrgs tries to unpack list organization from JSON document returned by D-Bus method GetOrgs.
// When it is possible to unmarshal the JSON document, then return list of organization keys (IDs).
// When it is not possible to get list of organizations, then return empty slice and error.
func unpackOrgs(s string) ([]string, error) {
var orgs []string
var organizations []Organization
err := json.Unmarshal([]byte(s), &organizations)
if err != nil {
return orgs, err
}
for _, org := range organizations {
orgs = append(orgs, org.Key)
}
return orgs, nil
}
// registerUsernamePassword tries to register system against candlepin server (Red Hat Management Service)
// username and password are mandatory. When organization is not obtained, then this method
// returns list of available organization and user can select one organization from the list.
func registerUsernamePassword(username, password, organization, serverURL string) ([]string, error) {
var orgs []string
if serverURL != "" {
if err := configureRHSM(serverURL); err != nil {
return orgs, fmt.Errorf("cannot configure RHSM: %w", err)
}
}
conn, err := dbus.SystemBus()
if err != nil {
return orgs, err
}
uuid, err := getConsumerUUID()
if err != nil {
return orgs, err
}
if uuid != "" {
return orgs, fmt.Errorf("warning: the system is already registered")
}
registerServer := conn.Object("com.redhat.RHSM1", "/com/redhat/RHSM1/RegisterServer")
locale := getLocale()
var privateDbusSocketURI string
if err := registerServer.Call(
"com.redhat.RHSM1.RegisterServer.Start",
dbus.Flags(0),
locale).Store(&privateDbusSocketURI); err != nil {
return orgs, err
}
defer registerServer.Call(
"com.redhat.RHSM1.RegisterServer.Stop",
dbus.FlagNoReplyExpected,
locale)
privConn, err := dbus.Dial(privateDbusSocketURI)
if err != nil {
return orgs, err
}
defer privConn.Close()
if err := privConn.Auth(nil); err != nil {
return orgs, err
}
if err := privConn.Object(
"com.redhat.RHSM1",
"/com/redhat/RHSM1/Register").Call(
"com.redhat.RHSM1.Register.Register",
dbus.Flags(0),
organization,
username,
password,
map[string]string{"enable_content": "true"},
map[string]string{},
locale).Err; err != nil {
// Try to unpack D-Bus method
err := unpackRHSMError(err)
// Is unpacked error RHSMError
rhsmError, ok := err.(RHSMError)
if !ok {
return orgs, err
}
// When organization was not specified, and it is required to specify it, then
// try to get list of available organizations
if organization == "" && rhsmError.Exception == "OrgNotSpecifiedException" {
var s string
orgsCall := privConn.Object(
"com.redhat.RHSM1",
"/com/redhat/RHSM1/Register",
).Call(
"com.redhat.RHSM1.Register.GetOrgs",
dbus.Flags(0),
username,
password,
map[string]string{},
locale,
)
err = orgsCall.Store(&s)
if err != nil {
return orgs, err
}
orgs, err = unpackOrgs(s)
return orgs, err
}
return orgs, unpackRHSMError(err)
}
return orgs, nil
}
func registerActivationKey(orgID string, activationKeys []string, serverURL string) error {
if serverURL != "" {
if err := configureRHSM(serverURL); err != nil {
return fmt.Errorf("cannot configure RHSM: %w", err)
}
}
conn, err := dbus.SystemBus()
if err != nil {
return err
}
uuid, err := getConsumerUUID()
if err != nil {
return err
}
if uuid != "" {
return fmt.Errorf("warning: the system is already registered")
}
registerServer := conn.Object("com.redhat.RHSM1", "/com/redhat/RHSM1/RegisterServer")
locale := getLocale()
var privateDbusSocketURI string
if err := registerServer.Call(
"com.redhat.RHSM1.RegisterServer.Start",
dbus.Flags(0),
locale).Store(&privateDbusSocketURI); err != nil {
return err
}
defer registerServer.Call(
"com.redhat.RHSM1.RegisterServer.Stop",
dbus.FlagNoReplyExpected,
locale)
privConn, err := dbus.Dial(privateDbusSocketURI)
if err != nil {
return err
}
defer privConn.Close()
if err := privConn.Auth(nil); err != nil {
return err
}
if err := privConn.Object(
"com.redhat.RHSM1",
"/com/redhat/RHSM1/Register").Call(
"com.redhat.RHSM1.Register.RegisterWithActivationKeys",
dbus.Flags(0),
orgID,
activationKeys,
map[string]string{},
map[string]string{},
locale).Err; err != nil {
return unpackRHSMError(err)
}
return nil
}
func unregister() error {
conn, err := dbus.SystemBus()
if err != nil {
return err
}
uuid, err := getConsumerUUID()
if err != nil {
return err
}
if uuid == "" {
return fmt.Errorf("warning: the system is already unregistered")
}
locale := getLocale()
err = conn.Object(
"com.redhat.RHSM1",
"/com/redhat/RHSM1/Unregister").Call(
"com.redhat.RHSM1.Unregister.Unregister",
dbus.Flags(0),
map[string]string{},
locale).Err
if err != nil {
return unpackRHSMError(err)
}
return nil
}
// RHSMError is used for parsing JSON document returned by D-Bus methods.
type RHSMError struct {
Exception string `json:"exception"`
Severity string `json:"severity"`
Message string `json:"message"`
}
// Error return textual representation of RHSMError. This implements all needed
// methods for error interface. Thus, RHSMError can be handled as regular error.
func (rhsmError RHSMError) Error() string {
return fmt.Sprintf("%v: %v", rhsmError.Severity, rhsmError.Message)
}
// unpackRHSMError tries to unpack JSON document (part of error) into the structure RHSMError. When it is
// not possible to parse error into structure, then corresponding or original error is returned.
// When it is possible to parse error into structure, then RHSMError is returned
func unpackRHSMError(err error) error {
rhsmError := RHSMError{}
switch e := err.(type) {
case dbus.Error:
if e.Name == "com.redhat.RHSM1.Error" {
if jsonErr := json.Unmarshal([]byte(e.Error()), &rhsmError); jsonErr != nil {
return jsonErr
}
return rhsmError
}
return fmt.Errorf("unable to parse D-Bus error due to unsupported error name: %s", e.Name)
}
return err
}
func configureRHSM(serverURL string) error {
if _, err := os.Stat("/etc/rhsm/rhsm.conf.orig"); os.IsNotExist(err) {
src, err := os.Open("/etc/rhsm/rhsm.conf")
if err != nil {
return fmt.Errorf("cannot open file for reading: %w", err)
}
defer src.Close()
dst, err := os.Create("/etc/rhsm/rhsm.conf.orig")
if err != nil {
return fmt.Errorf("cannot open file for writing: %w", err)
}
defer dst.Close()
if _, err := io.Copy(dst, src); err != nil {
return fmt.Errorf("cannot backup rhsm.conf: %w", err)
}
src.Close()
dst.Close()
}
URL, err := url.Parse(serverURL)
if err != nil {
return fmt.Errorf("cannot parse URL: %w", err)
}
conn, err := dbus.SystemBus()
if err != nil {
return fmt.Errorf("cannot connect to system D-Bus: %w", err)
}
locale := getLocale()
config := conn.Object("com.redhat.RHSM1", "/com/redhat/RHSM1/Config")
// If the scheme is empty, attempt to set the server.hostname based on the
// path component alone. This enables the --server argument to accept just a
// host name without a full URI.
if URL.Scheme == "" {
if URL.Path != "" {
if err := config.Call(
"com.redhat.RHSM1.Config.Set",
0,
"server.hostname",
URL.Path,
locale).Err; err != nil {
return unpackRHSMError(err)
}
}
} else {
if URL.Hostname() != "" {
if err := config.Call(
"com.redhat.RHSM1.Config.Set",
0,
"server.hostname",
URL.Hostname(),
locale).Err; err != nil {
return unpackRHSMError(err)
}
}
if URL.Port() != "" {
if err := config.Call(
"com.redhat.RHSM1.Config.Set",
0,
"server.port",
URL.Port(),
locale).Err; err != nil {
return unpackRHSMError(err)
}
}
if URL.Path != "" {
if err := config.Call(
"com.redhat.RHSM1.Config.Set",
0,
"server.prefix",
URL.Path,
locale).Err; err != nil {
return unpackRHSMError(err)
}
}
}
return nil
}
func getRHSMConfigOption(name string, val interface{}) error {
conn, err := dbus.SystemBus()
if err != nil {
return fmt.Errorf("cannot connect to system D-Bus: %w", err)
}
locale := getLocale()
obj := conn.Object("com.redhat.RHSM1", "/com/redhat/RHSM1/Config")
if err := obj.Call(
"com.redhat.RHSM1.Config.Get",
dbus.Flags(0),
name,
locale).Store(val); err != nil {
return unpackRHSMError(err)
}
return nil
}