Skip to content

Commit be2b4c0

Browse files
committed
Proof of concept for sending profile data
Signed-off-by: Miquel Sabaté Solà <msabate@suse.com>
1 parent a98647f commit be2b4c0

File tree

15 files changed

+86
-34
lines changed

15 files changed

+86
-34
lines changed

cmd/public-api-demo/main.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func runDemo(identifier, version, arch, regcode string) error {
7777

7878
connection.AddRegcodeAuth(request, regcode)
7979

80-
payload, err := conn.Do(request)
80+
_, payload, err := conn.Do(request)
8181
if err != nil {
8282
return err
8383
}
@@ -109,7 +109,9 @@ func runDemo(identifier, version, arch, regcode string) error {
109109
"instance_data": "<document>{}</document>",
110110
}
111111

112-
status, statusErr := registration.Status(conn, hostname, systemInformation, extraData)
112+
profiles := registration.DataProfiles{}
113+
114+
status, statusErr := registration.Status(conn, hostname, systemInformation, profiles, extraData)
113115
if statusErr != nil {
114116
return statusErr
115117
}

internal/connect/client.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ func IsOutdatedRegProxy(conn connection.Connection, opts *Options) bool {
337337
return true
338338
}
339339

340-
_, err = conn.Do(req)
340+
_, _, err = conn.Do(req)
341341
if err == nil {
342342
return true
343343
}
@@ -418,7 +418,7 @@ func InstallerUpdates(conn connection.Connection, product registration.Product)
418418
}
419419
req = connection.AddQuery(req, product.ToQuery())
420420

421-
resp, err := conn.Do(req)
421+
_, resp, err := conn.Do(req)
422422
if err != nil {
423423
return repos, err
424424
}
@@ -451,7 +451,7 @@ func SyncProducts(conn connection.Connection, products []registration.Product) (
451451

452452
connection.AddSystemAuth(request, login, password)
453453

454-
response, doErr := conn.Do(request)
454+
_, response, doErr := conn.Do(request)
455455
if doErr != nil {
456456
return remoteProducts, doErr
457457
}
@@ -500,7 +500,7 @@ func updateMigrations(conn connection.Connection, url string, payload any) ([]Mi
500500

501501
connection.AddSystemAuth(request, login, password)
502502

503-
response, doErr := conn.Do(request)
503+
_, response, doErr := conn.Do(request)
504504
if doErr != nil {
505505
return migrations, doErr
506506
}

internal/connect/wrapper.go

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package connect
22

33
import (
4+
"crypto/sha256"
5+
"encoding/hex"
46
"fmt"
57
"net/http"
68
"net/url"
@@ -102,6 +104,33 @@ func NewWrappedAPI(opts *Options) WrappedAPI {
102104
}
103105
}
104106

107+
// TODO: NOTE: just for testing purposes. It's a quick and dirty implementation
108+
// of a packages list collection. No caching, no fancy business :-)
109+
func FetchSystemProfiles() (registration.DataProfiles, error) {
110+
output, err := util.Execute([]string{"rpm", "-qa"}, nil)
111+
if err != nil {
112+
return registration.DataProfiles{}, err
113+
}
114+
115+
hash := sha256.Sum256(output)
116+
checksum := hex.EncodeToString(hash[:])
117+
118+
data := ""
119+
if os.Getenv("CONNECT_PROFILE_FULL") == "1" {
120+
data = string(output)
121+
}
122+
123+
packages := map[string]string{
124+
"checksum": checksum,
125+
"data": data,
126+
}
127+
result := registration.DataProfiles{
128+
"packages": packages,
129+
}
130+
131+
return result, nil
132+
}
133+
105134
// Submit a keepalive request to the server pointed by the configured
106135
// connection.
107136
func (w Wrapper) KeepAlive(uptimeTracking bool) error {
@@ -122,8 +151,15 @@ func (w Wrapper) KeepAlive(uptimeTracking bool) error {
122151
extraData["online_at"] = data
123152
}
124153

125-
code, err := registration.Status(w.Connection, hostname, hwinfo, extraData)
126-
if code != registration.Registered {
154+
profiles, err := FetchSystemProfiles()
155+
if err != nil {
156+
return fmt.Errorf("could not fetch system's profiles: %v", err)
157+
}
158+
159+
code, err := registration.Status(w.Connection, hostname, hwinfo, profiles, extraData)
160+
if code == registration.ClearCache {
161+
fmt.Printf("CACHE SHOULD BE CLEARED!\n")
162+
} else if code != registration.Registered {
127163
return fmt.Errorf("trying to send a keepalive from a system not yet registered. Register this system first")
128164
}
129165
return err

pkg/connection/connection.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ type Connection interface {
2424
// Builds a http.Request and setup up headers. The body can provided as io.Reader
2525
BuildRequestRaw(verb string, path string, body io.Reader) (*http.Request, error)
2626

27-
// Performs an HTTP request to the remote API. Returns the response body or
28-
// an error object.
29-
Do(*http.Request) ([]byte, error)
27+
// Performs an HTTP request to the remote API. Returns the status code and
28+
// the response body, or an error object.
29+
Do(*http.Request) (int, []byte, error)
3030

3131
// Returns the credentials object to be used for authenticated requests.
3232
GetCredentials() Credentials
@@ -77,35 +77,35 @@ func (conn ApiConnection) BuildRequestRaw(verb string, path string, body io.Read
7777
return request, nil
7878
}
7979

80-
func (conn ApiConnection) Do(request *http.Request) ([]byte, error) {
80+
func (conn ApiConnection) Do(request *http.Request) (int, []byte, error) {
8181
token, tokenErr := conn.Credentials.Token()
8282
if tokenErr != nil {
83-
return []byte{}, tokenErr
83+
return 0, []byte{}, tokenErr
8484
}
8585
request.Header.Set("System-Token", token)
8686

8787
response, doErr := conn.setupHTTPClient().Do(request)
8888
if doErr != nil {
89-
return nil, doErr
89+
return 0, nil, doErr
9090
}
9191
defer response.Body.Close()
9292

9393
// Update the credentials from the new system token.
9494
token = response.Header.Get("System-Token")
9595
if err := conn.Credentials.UpdateToken(token); err != nil {
96-
return nil, err
96+
return 0, nil, err
9797
}
9898

9999
// Check if there was an error from the given API response.
100100
if apiError := ErrorFromResponse(response); apiError != nil {
101-
return nil, apiError
101+
return 0, nil, apiError
102102
}
103103

104104
data, readErr := io.ReadAll(response.Body)
105105
if readErr != nil {
106-
return nil, readErr
106+
return 0, nil, readErr
107107
}
108-
return data, nil
108+
return response.StatusCode, data, nil
109109
}
110110

111111
func (conn ApiConnection) GetCredentials() Credentials {

pkg/labels/assign.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func AssignLabels(conn connection.Connection, labels []Label) ([]Label, error) {
3131

3232
connection.AddSystemAuth(request, login, password)
3333

34-
response, doErr := conn.Do(request)
34+
_, response, doErr := conn.Do(request)
3535
if doErr != nil {
3636
return []Label{}, doErr
3737
}

pkg/labels/list.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func ListLabels(conn connection.Connection) ([]Label, error) {
2222

2323
connection.AddSystemAuth(request, login, password)
2424

25-
response, doErr := conn.Do(request)
25+
_, response, doErr := conn.Do(request)
2626
if doErr != nil {
2727
return []Label{}, doErr
2828
}

pkg/labels/unassign.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func UnassignLabel(conn connection.Connection, labelId int) ([]Label, error) {
2727

2828
connection.AddSystemAuth(request, login, password)
2929

30-
response, doErr := conn.Do(request)
30+
_, response, doErr := conn.Do(request)
3131
if doErr != nil {
3232
return []Label{}, doErr
3333
}

pkg/registration/activate.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func doActivateCall(conn connection.Connection, verb, identifier, version, arch,
8585

8686
connection.AddSystemAuth(request, login, password)
8787

88-
response, doErr := conn.Do(request)
88+
_, response, doErr := conn.Do(request)
8989
if doErr != nil {
9090
return nil, &Product{}, doErr
9191
}

pkg/registration/activation.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func FetchActivations(conn connection.Connection) ([]*Activation, error) {
5252

5353
connection.AddSystemAuth(request, login, password)
5454

55-
response, doErr := conn.Do(request)
55+
_, response, doErr := conn.Do(request)
5656
if doErr != nil {
5757
return []*Activation{}, doErr
5858
}

pkg/registration/offline_request.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ func RegisterWithOfflineRequest(conn connection.Connection, regcode string, offl
8686
// the API is not parsing the request blob as compressed JSON
8787
request.Header.Set("Content-Type", "text/plain")
8888

89-
response, doErr := conn.Do(request)
89+
_, response, doErr := conn.Do(request)
9090
if doErr != nil {
9191
return nil, doErr
9292
}

0 commit comments

Comments
 (0)