mirror of
https://github.com/Control-D-Inc/ctrld.git
synced 2026-07-16 13:17:19 +02:00
internal/controld: add support for parsing client id from raw UID
This commit is contained in:
committed by
Cuong Manh Le
parent
19bc44a7f3
commit
72d2f4e7e3
@@ -8,6 +8,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/Control-D-Inc/ctrld"
|
"github.com/Control-D-Inc/ctrld"
|
||||||
@@ -53,12 +54,18 @@ func (u UtilityErrorResponse) Error() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type utilityRequest struct {
|
type utilityRequest struct {
|
||||||
UID string `json:"uid"`
|
UID string `json:"uid"`
|
||||||
|
ClientID string `json:"client_id,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// FetchResolverConfig fetch Control D config for given uid.
|
// FetchResolverConfig fetch Control D config for given uid.
|
||||||
func FetchResolverConfig(uid, version string, cdDev bool) (*ResolverConfig, error) {
|
func FetchResolverConfig(rawUID, version string, cdDev bool) (*ResolverConfig, error) {
|
||||||
body, _ := json.Marshal(utilityRequest{UID: uid})
|
uid, clientID := ParseRawUID(rawUID)
|
||||||
|
uReq := utilityRequest{UID: uid}
|
||||||
|
if clientID != "" {
|
||||||
|
uReq.ClientID = clientID
|
||||||
|
}
|
||||||
|
body, _ := json.Marshal(uReq)
|
||||||
apiUrl := resolverDataURLCom
|
apiUrl := resolverDataURLCom
|
||||||
if cdDev {
|
if cdDev {
|
||||||
apiUrl = resolverDataURLDev
|
apiUrl = resolverDataURLDev
|
||||||
@@ -120,3 +127,13 @@ func FetchResolverConfig(uid, version string, cdDev bool) (*ResolverConfig, erro
|
|||||||
}
|
}
|
||||||
return &ur.Body.Resolver, nil
|
return &ur.Body.Resolver, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ParseRawUID parse the input raw UID, returning real UID and ClientID.
|
||||||
|
// The raw UID can have 2 forms:
|
||||||
|
//
|
||||||
|
// - <uid>
|
||||||
|
// - <uid>/<client_id>
|
||||||
|
func ParseRawUID(rawUID string) (string, string) {
|
||||||
|
uid, clientID, _ := strings.Cut(rawUID, "/")
|
||||||
|
return uid, clientID
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,34 +1,31 @@
|
|||||||
//go:build controld
|
|
||||||
|
|
||||||
package controld
|
package controld
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestFetchResolverConfig(t *testing.T) {
|
func Test_parseUID(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
uid string
|
uid string
|
||||||
dev bool
|
wantUID string
|
||||||
wantErr bool
|
wantClientID string
|
||||||
}{
|
}{
|
||||||
{"valid com", "p2", false, false},
|
{"empty", "", "", ""},
|
||||||
{"valid dev", "p2", true, false},
|
{"only uid", "abcd1234", "abcd1234", ""},
|
||||||
{"invalid uid", "abcd1234", false, true},
|
{"with client id", "abcd1234/clientID", "abcd1234", "clientID"},
|
||||||
|
{"with empty clientID", "abcd1234/", "abcd1234", ""},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tc := range tests {
|
for _, tc := range tests {
|
||||||
tc := tc
|
tc := tc
|
||||||
t.Run(tc.name, func(t *testing.T) {
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
got, err := FetchResolverConfig(tc.uid, "dev-test", tc.dev)
|
gotUID, gotClientID := ParseRawUID(tc.uid)
|
||||||
require.False(t, (err != nil) != tc.wantErr, err)
|
assert.Equal(t, tc.wantUID, gotUID)
|
||||||
if !tc.wantErr {
|
assert.Equal(t, tc.wantClientID, gotClientID)
|
||||||
assert.NotEmpty(t, got.DOH)
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,34 @@
|
|||||||
|
//go:build controld
|
||||||
|
|
||||||
|
package controld
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestFetchResolverConfig(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
uid string
|
||||||
|
dev bool
|
||||||
|
wantErr bool
|
||||||
|
}{
|
||||||
|
{"valid com", "p2", false, false},
|
||||||
|
{"valid dev", "p2", true, false},
|
||||||
|
{"invalid uid", "abcd1234", false, true},
|
||||||
|
}
|
||||||
|
for _, tc := range tests {
|
||||||
|
tc := tc
|
||||||
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
got, err := FetchResolverConfig(tc.uid, "dev-test", tc.dev)
|
||||||
|
require.False(t, (err != nil) != tc.wantErr, err)
|
||||||
|
if !tc.wantErr {
|
||||||
|
assert.NotEmpty(t, got.DOH)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user