From 0cebe1e57e7e4b03aef77cd11bd4fc683c32afc6 Mon Sep 17 00:00:00 2001 From: Michael Muré Date: Sun, 23 Feb 2020 14:05:03 +0100 Subject: bridge: record the login used during the configure and use it as default credential fix #338 --- bridge/github/config.go | 6 ++++-- bridge/github/export.go | 47 +++++++++++++++++++------------------------- bridge/github/export_test.go | 10 ++++++---- bridge/github/github.go | 5 +++-- bridge/github/import.go | 10 +++++++--- bridge/github/import_test.go | 5 +++-- 6 files changed, 43 insertions(+), 40 deletions(-) (limited to 'bridge/github') diff --git a/bridge/github/config.go b/bridge/github/config.go index 269c6144..0093ec38 100644 --- a/bridge/github/config.go +++ b/bridge/github/config.go @@ -126,6 +126,7 @@ func (g *Github) Configure(repo *cache.RepoCache, params core.BridgeParams) (cor conf[core.ConfigKeyTarget] = target conf[confKeyOwner] = owner conf[confKeyProject] = project + conf[confKeyDefaultLogin] = login err = g.ValidateConfig(conf) if err != nil { @@ -149,14 +150,15 @@ func (*Github) ValidateConfig(conf core.Configuration) error { } else if v != target { return fmt.Errorf("unexpected target name: %v", v) } - if _, ok := conf[confKeyOwner]; !ok { return fmt.Errorf("missing %s key", confKeyOwner) } - if _, ok := conf[confKeyProject]; !ok { return fmt.Errorf("missing %s key", confKeyProject) } + if _, ok := conf[confKeyDefaultLogin]; !ok { + return fmt.Errorf("missing %s key", confKeyDefaultLogin) + } return nil } diff --git a/bridge/github/export.go b/bridge/github/export.go index 12b62fa6..b939d878 100644 --- a/bridge/github/export.go +++ b/bridge/github/export.go @@ -35,10 +35,13 @@ type githubExporter struct { identityClient map[entity.Id]*githubv4.Client // the client to use for non user-specific queries - // should be the client of the default user + // it's the client associated to the "default-login" config + // used for the github V4 API (graphql) defaultClient *githubv4.Client // the token of the default user + // it's the token associated to the "default-login" config + // used for the github V3 API (REST) defaultToken *auth.Token // github repository ID @@ -59,34 +62,12 @@ func (ge *githubExporter) Init(_ context.Context, repo *cache.RepoCache, conf co ge.cachedOperationIDs = make(map[entity.Id]string) ge.cachedLabels = make(map[string]string) - user, err := repo.GetUserIdentity() - if err != nil { - return err - } - // preload all clients - err = ge.cacheAllClient(repo) + err := ge.cacheAllClient(repo) if err != nil { return err } - ge.defaultClient, err = ge.getClientForIdentity(user.Id()) - if err != nil { - return err - } - - login := user.ImmutableMetadata()[metaKeyGithubLogin] - creds, err := auth.List(repo, auth.WithMeta(auth.MetaKeyLogin, login), auth.WithTarget(target), auth.WithKind(auth.KindToken)) - if err != nil { - return err - } - - if len(creds) == 0 { - return ErrMissingIdentityToken - } - - ge.defaultToken = creds[0].(*auth.Token) - return nil } @@ -111,12 +92,24 @@ func (ge *githubExporter) cacheAllClient(repo *cache.RepoCache) error { return nil } - if _, ok := ge.identityClient[user.Id()]; !ok { - client := buildClient(creds[0].(*auth.Token)) - ge.identityClient[user.Id()] = client + if _, ok := ge.identityClient[user.Id()]; ok { + continue + } + + client := buildClient(creds[0].(*auth.Token)) + ge.identityClient[user.Id()] = client + + // assign the default client and token as well + if ge.defaultClient == nil && login == ge.conf[confKeyDefaultLogin] { + ge.defaultClient = client + ge.defaultToken = creds[0].(*auth.Token) } } + if ge.defaultClient == nil { + return fmt.Errorf("no token found for the default login \"%s\"", ge.conf[confKeyDefaultLogin]) + } + return nil } diff --git a/bridge/github/export_test.go b/bridge/github/export_test.go index acbd657a..0748ecbf 100644 --- a/bridge/github/export_test.go +++ b/bridge/github/export_test.go @@ -190,8 +190,9 @@ func TestPushPull(t *testing.T) { // initialize exporter exporter := &githubExporter{} err = exporter.Init(ctx, backend, core.Configuration{ - confKeyOwner: envUser, - confKeyProject: projectName, + confKeyOwner: envUser, + confKeyProject: projectName, + confKeyDefaultLogin: login, }) require.NoError(t, err) @@ -217,8 +218,9 @@ func TestPushPull(t *testing.T) { importer := &githubImporter{} err = importer.Init(ctx, backend, core.Configuration{ - confKeyOwner: envUser, - confKeyProject: projectName, + confKeyOwner: envUser, + confKeyProject: projectName, + confKeyDefaultLogin: login, }) require.NoError(t, err) diff --git a/bridge/github/github.go b/bridge/github/github.go index 3a99cec7..1e85eb9a 100644 --- a/bridge/github/github.go +++ b/bridge/github/github.go @@ -19,8 +19,9 @@ const ( metaKeyGithubUrl = "github-url" metaKeyGithubLogin = "github-login" - confKeyOwner = "owner" - confKeyProject = "project" + confKeyOwner = "owner" + confKeyProject = "project" + confKeyDefaultLogin = "default-login" githubV3Url = "https://api.github.com" defaultTimeout = 60 * time.Second diff --git a/bridge/github/import.go b/bridge/github/import.go index b30be73a..58fec98c 100644 --- a/bridge/github/import.go +++ b/bridge/github/import.go @@ -19,7 +19,7 @@ import ( type githubImporter struct { conf core.Configuration - // default user client + // default client client *githubv4.Client // iterator @@ -32,7 +32,11 @@ type githubImporter struct { func (gi *githubImporter) Init(_ context.Context, repo *cache.RepoCache, conf core.Configuration) error { gi.conf = conf - creds, err := auth.List(repo, auth.WithTarget(target), auth.WithKind(auth.KindToken)) + creds, err := auth.List(repo, + auth.WithTarget(target), + auth.WithKind(auth.KindToken), + auth.WithMeta(auth.MetaKeyLogin, conf[confKeyDefaultLogin]), + ) if err != nil { return err } @@ -434,7 +438,7 @@ func (gi *githubImporter) ensureTimelineComment(repo *cache.RepoCache, b *cache. } gi.out <- core.NewImportComment(op.Id()) - // set target for the nexr edit now that the comment is created + // set target for the next edit now that the comment is created targetOpID = op.Id() continue } diff --git a/bridge/github/import_test.go b/bridge/github/import_test.go index 20b1b71e..107d74c5 100644 --- a/bridge/github/import_test.go +++ b/bridge/github/import_test.go @@ -153,8 +153,9 @@ func Test_Importer(t *testing.T) { importer := &githubImporter{} err = importer.Init(ctx, backend, core.Configuration{ - confKeyOwner: "MichaelMure", - confKeyProject: "git-bug-test-github-bridge", + confKeyOwner: "MichaelMure", + confKeyProject: "git-bug-test-github-bridge", + confKeyDefaultLogin: login, }) require.NoError(t, err) -- cgit