From c86e7231b223d532e26ab5449715c65b6b4e3fde Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Mur=C3=A9?= Date: Mon, 24 Sep 2018 17:21:24 +0200 Subject: [PATCH] bridge: validate config before use --- bridge/core/bridge.go | 5 +++++ bridge/core/interfaces.go | 3 +++ bridge/github/config.go | 16 ++++++++++++++++ 3 files changed, 24 insertions(+) diff --git a/bridge/core/bridge.go b/bridge/core/bridge.go index d9d688ba05aeb3f8376bd52d7d6e492050441352..dd1f13372c464d09af8e11b67da7ce2227153b0a 100644 --- a/bridge/core/bridge.go +++ b/bridge/core/bridge.go @@ -200,6 +200,11 @@ func (b Bridge) loadConfig() (Configuration, error) { result[key] = value } + err = b.impl.ValidateConfig(result) + if err != nil { + return nil, errors.Wrap(err, "invalid configuration") + } + return result, nil } diff --git a/bridge/core/interfaces.go b/bridge/core/interfaces.go index 5ead3c8f1695ca2f55fd2d4dec2abb5f51dc537e..79b75606facd62fe274b50a8ade2cf6809c5c843 100644 --- a/bridge/core/interfaces.go +++ b/bridge/core/interfaces.go @@ -15,6 +15,9 @@ type BridgeImpl interface { // for future use Configure(repo repository.RepoCommon) (Configuration, error) + // ValidateConfig check the configuration for error + ValidateConfig(conf Configuration) error + // Importer return an Importer implementation if the import is supported Importer() Importer diff --git a/bridge/github/config.go b/bridge/github/config.go index b8531dfe61fe3290fdb9a28e5a2274233a91870d..79025cfb462faf9e384246b22cd2ea0322713645 100644 --- a/bridge/github/config.go +++ b/bridge/github/config.go @@ -93,6 +93,22 @@ func (*Github) Configure(repo repository.RepoCommon) (core.Configuration, error) return nil, nil } +func (*Github) ValidateConfig(conf core.Configuration) error { + if _, ok := conf[keyToken]; !ok { + return fmt.Errorf("missing %s key", keyToken) + } + + if _, ok := conf[keyUser]; !ok { + return fmt.Errorf("missing %s key", keyUser) + } + + if _, ok := conf[keyProject]; !ok { + return fmt.Errorf("missing %s key", keyProject) + } + + return nil +} + func requestToken(note, username, password string) (*http.Response, error) { return requestTokenWith2FA(note, username, password, "") }