17295: Adds cluster ID validation on the config file.
authorLucas Di Pentima <lucas@di-pentima.com.ar>
Fri, 19 Feb 2021 15:48:32 +0000 (12:48 -0300)
committerPeter Amstutz <peter.amstutz@curii.com>
Mon, 22 Feb 2021 15:00:31 +0000 (10:00 -0500)
Arvados-DCO-1.1-Signed-off-by: Lucas Di Pentima <lucas@di-pentima.com.ar>

lib/config/export_test.go
lib/config/load.go
lib/config/load_test.go

index 7af117e385f405dbe5f0e8acb7d597f55ab77fe4..f11b65f452b797a0cbe05720f29c51343baa7779 100644 (file)
@@ -17,7 +17,7 @@ var _ = check.Suite(&ExportSuite{})
 type ExportSuite struct{}
 
 func (s *ExportSuite) TestExport(c *check.C) {
-       confdata := strings.Replace(string(DefaultYAML), "SAMPLE", "testkey", -1)
+       confdata := strings.Replace(string(DefaultYAML), "SAMPLE", "12345", -1)
        cfg, err := testLoader(c, confdata, nil).Load()
        c.Assert(err, check.IsNil)
        cluster, err := cfg.GetCluster("xxxxx")
index be6181bbe9bbc033cd9241cc14a8eca36ed23610..f682359379f747fbbdd571b3653485e3ece87eb4 100644 (file)
@@ -13,6 +13,7 @@ import (
        "io"
        "io/ioutil"
        "os"
+       "regexp"
        "strings"
 
        "git.arvados.org/arvados.git/sdk/go/arvados"
@@ -269,7 +270,21 @@ func (ldr *Loader) Load() (*arvados.Config, error) {
 
        // Check for known mistakes
        for id, cc := range cfg.Clusters {
+               for remote, _ := range cc.RemoteClusters {
+                       if remote == "*" || remote == "SAMPLE" {
+                               continue
+                       }
+                       err = ldr.checkClusterID(fmt.Sprintf("Clusters.%s.RemoteClusters.%s", id, remote), remote, true)
+                       if err != nil {
+                               return nil, err
+                       }
+               }
                for _, err = range []error{
+                       ldr.checkClusterID(fmt.Sprintf("Clusters.%s", id), id, false),
+                       ldr.checkClusterID(fmt.Sprintf("Clusters.%s.Login.LoginCluster", id), cc.Login.LoginCluster, true),
+                       ldr.checkToken(fmt.Sprintf("Clusters.%s.ManagementToken", id), cc.ManagementToken),
+                       ldr.checkToken(fmt.Sprintf("Clusters.%s.SystemRootToken", id), cc.SystemRootToken),
+                       ldr.checkToken(fmt.Sprintf("Clusters.%s.Collections.BlobSigningKey", id), cc.Collections.BlobSigningKey),
                        checkKeyConflict(fmt.Sprintf("Clusters.%s.PostgreSQL.Connection", id), cc.PostgreSQL.Connection),
                        ldr.checkEmptyKeepstores(cc),
                        ldr.checkUnlistedKeepstores(cc),
@@ -282,6 +297,31 @@ func (ldr *Loader) Load() (*arvados.Config, error) {
        return &cfg, nil
 }
 
+var acceptableClusterIDRe = regexp.MustCompile(`^[a-z0-9]{5}$`)
+
+func (ldr *Loader) checkClusterID(label, clusterID string, emptyStringOk bool) error {
+       if emptyStringOk && clusterID == "" {
+               return nil
+       } else if !acceptableClusterIDRe.MatchString(clusterID) {
+               return fmt.Errorf("%s: cluster ID should be 5 alphanumeric characters", label)
+       }
+       return nil
+}
+
+var acceptableTokenRe = regexp.MustCompile(`^[a-zA-Z0-9]+$`)
+var acceptableTokenLength = 32
+
+func (ldr *Loader) checkToken(label, token string) error {
+       if token == "" {
+               ldr.Logger.Warnf("%s: secret token is not set (use %d+ random characters from a-z, A-Z, 0-9)", label, acceptableTokenLength)
+       } else if !acceptableTokenRe.MatchString(token) {
+               return fmt.Errorf("%s: unacceptable characters in token (only a-z, A-Z, 0-9 are acceptable)", label)
+       } else if len(token) < acceptableTokenLength {
+               ldr.Logger.Warnf("%s: token is too short (should be at least %d characters)", label, acceptableTokenLength)
+       }
+       return nil
+}
+
 func checkKeyConflict(label string, m map[string]string) error {
        saw := map[string]bool{}
        for k := range m {
index 303f1d9598690718f44a1398b602c89eb8137c33..0fe40a69e24b7831448e45e949c2edb736d5e579 100644 (file)
@@ -312,7 +312,7 @@ Clusters:
     Proxy: true
 `, `
 Clusters:
- abcdef:
+ abcde:
   ManagementToken: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
   SystemRootToken: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
   Collections: