func TestKontrolMultiKey(t *testing.T) { if storage := os.Getenv("KONTROL_STORAGE"); storage != "postgres" { t.Skip("%q storage does not currently implement soft key pair deletes", storage) } i := uuid.NewV4() secondID := i.String() // add so we can use it as key if err := kon.AddKeyPair(secondID, testkeys.PublicSecond, testkeys.PrivateSecond); err != nil { t.Fatal(err) } // Start mathworker mathKite := kite.New("mathworker2", "2.0.0") mathKite.Config = conf.Config.Copy() mathKite.Config.Port = 6162 mathKite.HandleFunc("square", Square) go mathKite.Run() <-mathKite.ServerReadyNotify() defer mathKite.Close() go mathKite.RegisterForever(&url.URL{Scheme: "http", Host: "127.0.0.1:" + strconv.Itoa(mathKite.Config.Port), Path: "/kite"}) <-mathKite.KontrolReadyNotify() // exp3 kite is the mathworker client. However it uses a different public // key exp3Kite := kite.New("exp3", "0.0.1") exp3Kite.Config = conf.Config.Copy() exp3Kite.Config.KiteKey = testutil.NewKiteKeyWithKeyPair(testkeys.PrivateSecond, testkeys.PublicSecond).Raw exp3Kite.Config.KontrolKey = testkeys.PublicSecond defer exp3Kite.Close() query := &protocol.KontrolQuery{ Username: exp3Kite.Kite().Username, Environment: exp3Kite.Kite().Environment, Name: "mathworker2", Version: "2.0.0", } // exp3 queries for mathkite kites, err := exp3Kite.GetKites(query) if err != nil { t.Fatal(err) } defer klose(kites) if len(kites) == 0 { t.Fatal("No mathworker available") } // exp3 connectes to mathworker remoteMathWorker := kites[0] err = remoteMathWorker.Dial() if err != nil { t.Fatal("Cannot connect to remote mathworker", err) } // Test Kontrol.GetToken // TODO(rjeczalik): rework test to not touch Kontrol internals kon.tokenCacheMu.Lock() kon.tokenCache = make(map[string]string) // empty it kon.tokenCacheMu.Unlock() newToken, err := exp3Kite.GetToken(&remoteMathWorker.Kite) if err != nil { t.Error(err) } if remoteMathWorker.Auth.Key == newToken { t.Errorf("Token renew failed. Tokens should be different after renew") } // Run "square" method response, err := remoteMathWorker.TellWithTimeout("square", 4*time.Second, 2) if err != nil { t.Fatal(err) } var result int err = response.Unmarshal(&result) if err != nil { t.Fatal(err) } // Result must be "4" if result != 4 { t.Fatalf("Invalid result: %d", result) } // now invalidate the second key log.Printf("Invalidating %s\n", secondID) if err := kon.DeleteKeyPair(secondID, ""); err != nil { t.Fatal(err) } // try to get a new key, this should replace exp3Kite.Config.KontrolKey // with the new (in our case because PublicSecond is invalidated, it's // going to use Public (the first key). Also it should return the new Key. publicKey, err := exp3Kite.GetKey() if err != nil { t.Fatal(err) } if publicKey != testkeys.Public { t.Errorf("Key renew failed\n\twant:%s\n\tgot :%s\n", testkeys.Public, publicKey) } if exp3Kite.Config.KontrolKey != publicKey { t.Errorf("Key renew should replace config.KontrolKey\n\twant:%s\n\tgot :%s\n", testkeys.Public, publicKey) } }
func (cfg *Config) setDefaults() { if cfg.TopDir == "" { p, err := exec.Command("git", "rev-parse", "--show-toplevel").Output() if err != nil { cfg.Log.Warning("unable to get git top dir:", err) } else { cfg.TopDir = string(bytes.TrimSpace(p)) } } if cfg.DockerHost == "" { if u, err := url.Parse(os.Getenv("DOCKER_HOST")); err == nil { if host, _, err := net.SplitHostPort(u.Host); err == nil { cfg.DockerHost = host } else { cfg.DockerHost = u.Host } } } if cfg.Username == "" { u, err := user.Current() if err == nil { cfg.Username = u.Username } } if cfg.KontrolPrivate == "" { cfg.KontrolPrivate = filepath.Join(cfg.TopDir, "certs", "test_kontrol_rsa_private.pem") p, err := ioutil.ReadFile(cfg.KontrolPrivate) if err == nil { cfg.pemPrivate = string(p) } } if cfg.KontrolPublic == "" { cfg.KontrolPublic = filepath.Join(cfg.TopDir, "certs", "test_kontrol_rsa_public.pem") p, err := ioutil.ReadFile(cfg.KontrolPublic) if err == nil { cfg.pemPublic = string(p) } } if cfg.pemPrivate == "" { cfg.pemPrivate = testkeys.Private } if cfg.pemPublic == "" { cfg.pemPublic = testkeys.Public } if cfg.Kontrol.KontrolURL == "" { cfg.Kontrol = config.Config{ Port: 4000, Username: "******", Environment: "dev", Region: "dev", KontrolUser: "******", KontrolKey: cfg.pemPublic, KiteKey: testutil.NewKiteKeyWithKeyPair(cfg.pemPrivate, cfg.pemPublic).Raw, KontrolURL: "http://localhost:4000/kite", } } if cfg.Postgres == (kontrol.PostgresConfig{}) { cfg.Postgres = kontrol.PostgresConfig{ Host: cfg.DockerHost, Port: 5432, Username: "******", Password: "******", DBName: "social", ConnectTimeout: 20, } } if cfg.HostedZone == "" { cfg.HostedZone = "dev.koding.io" } }