Skip to content

Commit

Permalink
Merge pull request #701 from tranchitella/men-4360
Browse files Browse the repository at this point in the history
MEN-4360: Extend the D-Bus API to return the server URL
  • Loading branch information
lluiscampos committed Jan 18, 2021
2 parents 37fc0ca + 26cf2a1 commit 68ee8e6
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 17 deletions.
34 changes: 31 additions & 3 deletions app/auth.go
Expand Up @@ -54,12 +54,14 @@ const (
<interface name="io.mender.Authentication1">
<method name="GetJwtToken">
<arg type="s" name="token" direction="out"/>
<arg type="s" name="server_url" direction="out"/>
</method>
<method name="FetchJwtToken">
<arg type="b" name="success" direction="out"/>
</method>
<signal name="JwtTokenStateChange">
<arg type="s" name="token"/>
<arg type="s" name="server_url"/>
</signal>
</interface>
</node>`
Expand Down Expand Up @@ -135,6 +137,7 @@ type menderAuthManagerService struct {
store store.Store
keyStore *store.Keystore
idSrc device.IdentityDataGetter
serverURL string
tenantToken client.AuthToken
}

Expand Down Expand Up @@ -163,6 +166,17 @@ func NewAuthManager(conf AuthManagerConfig) *MenderAuthManager {
}
}

// get the first server URL available in the config file
serverURL := ""
if conf.Config != nil {
serverIterator := nextServerIterator(*conf.Config)
if serverIterator != nil {
if server := serverIterator(); server != nil {
serverURL = server.ServerURL
}
}
}

mgr := &MenderAuthManager{
&menderAuthManagerService{
inChan: make(chan AuthManagerRequest, authManagerInMessageChanSize),
Expand All @@ -177,6 +191,7 @@ func NewAuthManager(conf AuthManagerConfig) *MenderAuthManager {
keyStore: conf.KeyStore,
idSrc: conf.IdentitySource,
tenantToken: client.AuthToken(conf.TenantToken),
serverURL: serverURL,
},
}

Expand Down Expand Up @@ -224,7 +239,11 @@ func (m *menderAuthManagerService) registerDBusCallbacks() (unregisterFunc func(
}
select {
case message := <-respChan:
return string(message.AuthToken), message.Error
tokenAndServerURL := dbus.TokenAndServerURL{
Token: string(message.AuthToken),
ServerURL: m.serverURL,
}
return tokenAndServerURL, message.Error
case <-time.After(5 * time.Second):
}
return string(noAuthToken), errors.New("timeout when calling GetJwtToken")
Expand Down Expand Up @@ -407,9 +426,13 @@ func (m *menderAuthManagerService) broadcast(message AuthManagerResponse) {
}
// emit signal on dbus, if available
if m.dbus != nil {
tokenAndServerURL := dbus.TokenAndServerURL{
Token: string(message.AuthToken),
ServerURL: m.serverURL,
}
m.dbus.EmitSignal(m.dbusConn, "", AuthManagerDBusPath,
AuthManagerDBusInterfaceName, AuthManagerDBusSignalJwtTokenStateChange,
string(message.AuthToken))
tokenAndServerURL)
}
}

Expand Down Expand Up @@ -461,8 +484,10 @@ func (m *menderAuthManagerService) fetchAuthToken() {
return
}

var serverURL string
for {
rsp, err = m.authReq.Request(m.api, server.ServerURL, m)
serverURL = server.ServerURL
rsp, err = m.authReq.Request(m.api, serverURL, m)

if err == nil {
// SUCCESS!
Expand Down Expand Up @@ -497,6 +522,9 @@ func (m *menderAuthManagerService) fetchAuthToken() {
return
}

// store the current server URL
m.serverURL = serverURL

log.Info("successfully received new authorization data")
return
}
Expand Down
4 changes: 2 additions & 2 deletions app/auth_test.go
@@ -1,4 +1,4 @@
// Copyright 2020 Northern.tech AS
// 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.
Expand Down Expand Up @@ -378,7 +378,7 @@ func TestMenderAuthorize(t *testing.T) {
AuthManagerDBusPath,
AuthManagerDBusInterfaceName,
AuthManagerDBusSignalJwtTokenStateChange,
mock.AnythingOfType("string"),
mock.AnythingOfType("dbus.TokenAndServerURL"),
).Return(nil)

dbusAPI.On("MainLoopQuit", dbusLoop)
Expand Down
8 changes: 7 additions & 1 deletion dbus/dbus.go
@@ -1,4 +1,4 @@
// Copyright 2020 Northern.tech AS
// 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.
Expand Down Expand Up @@ -59,6 +59,12 @@ type DBusAPI interface {
// MethodCallCallback represents a method_call callback
type MethodCallCallback = func(objectPath string, interfaceName string, methodName string) (interface{}, error)

// TokenAndServerURL stores values for the JWT token and the server URL
type TokenAndServerURL struct {
Token string
ServerURL string
}

// GetDBusAPI returns the global DBusAPI object
func GetDBusAPI() (DBusAPI, error) {
if dbusAPI != nil {
Expand Down
10 changes: 8 additions & 2 deletions dbus/dbus_libgio.go
@@ -1,4 +1,4 @@
// Copyright 2020 Northern.tech AS
// 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.
Expand Down Expand Up @@ -185,7 +185,13 @@ func (d *dbusAPILibGio) EmitSignal(conn Handle, destinationBusName string, objec
}

func interfaceToGVariant(result interface{}) *C.GVariant {
if v, ok := result.(string); ok {
if v, ok := result.(TokenAndServerURL); ok {
strToken := C.CString(v.Token)
strServerURL := C.CString(v.ServerURL)
defer C.free(unsafe.Pointer(strToken))
defer C.free(unsafe.Pointer(strServerURL))
return C.g_variant_new_from_two_strings((*C.gchar)(strToken), (*C.gchar)(strServerURL))
} else if v, ok := result.(string); ok {
str := C.CString(v)
defer C.free(unsafe.Pointer(str))
return C.g_variant_new_from_string((*C.gchar)(str))
Expand Down
6 changes: 6 additions & 0 deletions dbus/dbus_libgio.go.h
Expand Up @@ -20,6 +20,12 @@ static GVariant *g_variant_new_from_string(gchar *value)
return g_variant_new("(s)", value);
}

// create a new GVariant from two string values
static GVariant *g_variant_new_from_two_strings(gchar *value1, gchar *value2)
{
return g_variant_new("(ss)", value1, value2);
}

// create a new GVariant from a boolean valule
static GVariant *g_variant_new_from_boolean(gboolean value)
{
Expand Down
49 changes: 40 additions & 9 deletions dbus/dbus_libgio_test.go
Expand Up @@ -75,6 +75,7 @@ func TestBusRegisterInterface(t *testing.T) {
<interface name="io.mender.Authentication1">
<method name="GetJwtToken">
<arg type="s" name="token" direction="out"/>
<arg type="s" name="server_url" direction="out"/>
</method>
<method name="FetchJwtToken">
<arg type="b" name="success" direction="out"/>
Expand All @@ -94,6 +95,7 @@ func TestBusRegisterInterface(t *testing.T) {
<interface name="io.mender.Authentication1">
<method name="GetJwtToken">
<arg type="s" name="token" direction="out"/>
<arg type="s" name="server_url" direction="out"/>
</method>
<method name="FetchJwtToken">
<arg type="b" name="success" direction="out"/>
Expand Down Expand Up @@ -165,10 +167,22 @@ func TestHandleMethodCallCallback(t *testing.T) {
assert.NoError(t, err)
defer godbusConn.Close()

xmlString := `<node>
<interface name="io.mender.Authentication1">
<method name="GetJwtToken">
<arg type="s" name="token" direction="out"/>
</method>
<method name="FetchJwtToken">
<arg type="b" name="success" direction="out"/>
</method>
</interface>
</node>`

xml := `<node>
<interface name="io.mender.Authentication1">
<method name="GetJwtToken">
<arg type="s" name="token" direction="out"/>
<arg type="s" name="server_url" direction="out"/>
</method>
<method name="FetchJwtToken">
<arg type="b" name="success" direction="out"/>
Expand All @@ -177,16 +191,17 @@ func TestHandleMethodCallCallback(t *testing.T) {
</node>`

testCases := map[string]struct {
xml string
path string
interfaceName string
methodName string
callback MethodCallCallback
outString string
outBoolean bool
xml string
path string
interfaceName string
methodName string
callback MethodCallCallback
outTokenAndServerURL *TokenAndServerURL
outString string
outBoolean bool
}{
"ok, string value": {
xml: xml,
xml: xmlString,
path: "/io/mender/AuthenticationManager/TestHandleMethodCallCallback1",
interfaceName: "io.mender.Authentication1",
methodName: "GetJwtToken",
Expand Down Expand Up @@ -215,6 +230,16 @@ func TestHandleMethodCallCallback(t *testing.T) {
},
outBoolean: false,
},
"ok, value": {
xml: xml,
path: "/io/mender/AuthenticationManager/TestHandleMethodCallCallback4",
interfaceName: "io.mender.Authentication1",
methodName: "GetJwtToken",
callback: func(objectPath, interfaceName, methodName string) (interface{}, error) {
return TokenAndServerURL{Token: "JWT_TOKEN"}, nil
},
outTokenAndServerURL: &TokenAndServerURL{Token: "JWT_TOKEN"},
},
}
for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
Expand All @@ -232,7 +257,13 @@ func TestHandleMethodCallCallback(t *testing.T) {

// client code, call the dbus method, isolated from the code above
func() {
if tc.outString != "" {
if tc.outTokenAndServerURL != nil {
var value string
interfaceMethodName := fmt.Sprintf("%s.%s", tc.interfaceName, tc.methodName)
err = godbusConn.Object(objectName, godbus.ObjectPath(tc.path)).Call(interfaceMethodName, 0).Store(&value)
assert.NoError(t, err)
assert.Equal(t, *tc.outTokenAndServerURL, value)
} else if tc.outString != "" {
var value string
interfaceMethodName := fmt.Sprintf("%s.%s", tc.interfaceName, tc.methodName)
err = godbusConn.Object(objectName, godbus.ObjectPath(tc.path)).Call(interfaceMethodName, 0).Store(&value)
Expand Down

0 comments on commit 68ee8e6

Please sign in to comment.