-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement HTTP connection in the public library
Introduce several interfaces which allow a caller to perform API requests against an SCC-like API. This includes: * Common options for the connection * Credentials handling * Building and performing requests * Automatically handle system token rotations Reviewed-by: Parag Jain <[email protected]> Signed-off-by: Felix Schnizlein <[email protected]> Signed-off-by: Miquel Sabaté Solà <[email protected]>
- Loading branch information
Showing
12 changed files
with
465 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package main | ||
|
||
import "fmt" | ||
|
||
type SccCredentials struct { | ||
SystemLogin string | ||
Password string | ||
SystemToken string | ||
} | ||
|
||
func (SccCredentials) HasAuthentication() bool { | ||
return true | ||
} | ||
|
||
func (creds *SccCredentials) Token() (string, error) { | ||
fmt.Printf("<- fetch token %s\n", creds.SystemToken) | ||
return creds.SystemToken, nil | ||
} | ||
|
||
func (creds *SccCredentials) UpdateToken(token string) error { | ||
fmt.Printf("-> update token %s\n", token) | ||
creds.SystemToken = token | ||
return nil | ||
} | ||
|
||
func (creds *SccCredentials) Login() (string, string, error) { | ||
if creds.SystemLogin == "" || creds.Password == "" { | ||
return "", "", fmt.Errorf("login credentials not set") | ||
} | ||
fmt.Printf("<- fetch login %s\n", creds.SystemLogin) | ||
return creds.SystemLogin, creds.Password, nil | ||
} | ||
|
||
func (creds *SccCredentials) SetLogin(login, password string) error { | ||
fmt.Printf("-> set login %s\n", login) | ||
creds.SystemLogin = login | ||
creds.Password = password | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,65 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/SUSE/connect-ng/pkg/connection" | ||
"github.com/SUSE/connect-ng/pkg/registration" | ||
"github.com/SUSE/connect-ng/pkg/validation" | ||
) | ||
|
||
type SccCredentials struct { | ||
Login string `json:"login"` | ||
Password string `json:"password"` | ||
SystemToken string `json:"system_token"` | ||
} | ||
const ( | ||
hostname = "public-api-demo" | ||
) | ||
|
||
func (SccCredentials) HasAuthentication() bool { | ||
return true | ||
func bold(format string, args ...interface{}) { | ||
fmt.Printf("\033[1m"+format+"\033[0m", args...) | ||
} | ||
|
||
func (creds *SccCredentials) Triplet() (string, string, string, error) { | ||
return creds.Login, creds.Password, creds.SystemToken, nil | ||
} | ||
func runDemo(regcode string) error { | ||
opts := connection.DefaultOptions("public-api-demo", "1.0", "DE") | ||
|
||
func (creds *SccCredentials) Load() error { | ||
creds = SccCredentials{ | ||
Login: "foo", | ||
Password: "bar", | ||
SystemToken: "", | ||
if url := os.Getenv("SCC_URL"); url != "" { | ||
opts.URL = url | ||
} | ||
return nil | ||
} | ||
|
||
func (creds *SccCredentials) Update(login, password, token string) error { | ||
creds = SccCredentials{ | ||
Login: login, | ||
Password: password, | ||
SystemToken: token, | ||
bold("1) Setup connection and perform an request\n") | ||
conn := connection.New(opts, &SccCredentials{}) | ||
|
||
request, buildErr := conn.BuildRequest("GET", "/connect/subscriptions/info", nil) | ||
if buildErr != nil { | ||
return buildErr | ||
} | ||
|
||
connection.AddRegcodeAuth(request, regcode) | ||
|
||
payload, err := conn.Do(request) | ||
if err != nil { | ||
return err | ||
} | ||
fmt.Printf("!! len(payload): %d characters\n", len(payload)) | ||
fmt.Printf("!! first 40 characters: %s\n", string(payload[0:40])) | ||
|
||
return nil | ||
} | ||
|
||
func main() { | ||
fmt.Println("I'm here") | ||
fmt.Println("public-api-demo: A connect client library demo") | ||
|
||
opts := connection.SCCOptions() | ||
if len(os.Args) != 4 { | ||
fmt.Println("./public-api-demo IDENTIFIER VERSION ARCH") | ||
return | ||
} | ||
|
||
// No authentication | ||
//_ = connection.New(opts, connection.NoCredentials{}) | ||
regcode := os.Getenv("REGCODE") | ||
if regcode == "" { | ||
fmt.Printf("ERROR: Requireing REGCODE to set as environment variable\n") | ||
os.Exit(1) | ||
} | ||
|
||
// With authentication | ||
conn := connection.New(opts, &SccCredentials{}) | ||
err := runDemo(regcode) | ||
|
||
_, _ = registration.Status(conn) | ||
_, _, _ = validation.OfflineActivation(bytes.NewReader([]byte{})) | ||
if err != nil { | ||
fmt.Printf("ERROR: %s\n", err) | ||
os.Exit(1) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package connection | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
) | ||
|
||
func AddRegcodeAuth(request *http.Request, regcode string) { | ||
tokenAuth := fmt.Sprintf("Token token=%s", regcode) | ||
|
||
request.Header.Set("Authorization", tokenAuth) | ||
} | ||
|
||
func AddSystemAuth(request *http.Request, login string, password string) { | ||
request.SetBasicAuth(login, password) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package connection | ||
|
||
import ( | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func testRequest(t *testing.T) *http.Request { | ||
assert := assert.New(t) | ||
|
||
opts := DefaultOptions("testApp", "1.0", "en_US") | ||
creds := NoCredentials{} | ||
conn := New(opts, creds) | ||
|
||
request, buildErr := conn.BuildRequest("GET", "/test/api", nil) | ||
assert.NoError(buildErr) | ||
|
||
return request | ||
} | ||
|
||
func TestAuthByRegcode(t *testing.T) { | ||
assert := assert.New(t) | ||
request := testRequest(t) | ||
|
||
regcode := "test" | ||
expected := "Token token=test" | ||
|
||
AddRegcodeAuth(request, regcode) | ||
assert.Equal(expected, request.Header.Get("Authorization")) | ||
} | ||
|
||
func TestAuthBySystemCredentials(t *testing.T) { | ||
assert := assert.New(t) | ||
request := testRequest(t) | ||
|
||
login := "login" | ||
password := "password" | ||
|
||
AddSystemAuth(request, login, password) | ||
assert.Equal("Basic bG9naW46cGFzc3dvcmQ=", request.Header.Get("Authorization")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.