agent: use an additional parameter for passing tokens

This commit is contained in:
Ryan Uber 2015-05-04 17:36:17 -07:00
parent 8d2834fb12
commit 2b62f2f172
8 changed files with 84 additions and 124 deletions

View File

@ -155,7 +155,7 @@ func Create(config *Config, logOutput io.Writer) (*Agent, error) {
Port: agent.config.Ports.Server, Port: agent.config.Ports.Server,
Tags: []string{}, Tags: []string{},
} }
agent.state.AddService(&consulService) agent.state.AddService(&consulService, "")
} else { } else {
err = agent.setupClient() err = agent.setupClient()
agent.state.SetIface(agent.client) agent.state.SetIface(agent.client)
@ -599,7 +599,7 @@ func (a *Agent) purgeCheck(checkID string) error {
// AddService is used to add a service entry. // AddService is used to add a service entry.
// This entry is persistent and the agent will make a best effort to // This entry is persistent and the agent will make a best effort to
// ensure it is registered // ensure it is registered
func (a *Agent) AddService(service *structs.NodeService, chkTypes CheckTypes, persist bool) error { func (a *Agent) AddService(service *structs.NodeService, chkTypes CheckTypes, persist bool, token string) error {
if service.Service == "" { if service.Service == "" {
return fmt.Errorf("Service name missing") return fmt.Errorf("Service name missing")
} }
@ -629,7 +629,7 @@ func (a *Agent) AddService(service *structs.NodeService, chkTypes CheckTypes, pe
} }
// Add the service // Add the service
a.state.AddService(service) a.state.AddService(service, token)
// Persist the service to a file // Persist the service to a file
if persist { if persist {
@ -653,7 +653,7 @@ func (a *Agent) AddService(service *structs.NodeService, chkTypes CheckTypes, pe
ServiceID: service.ID, ServiceID: service.ID,
ServiceName: service.Service, ServiceName: service.Service,
} }
if err := a.AddCheck(check, chkType, persist); err != nil { if err := a.AddCheck(check, chkType, persist, token); err != nil {
return err return err
} }
} }
@ -704,7 +704,7 @@ func (a *Agent) RemoveService(serviceID string, persist bool) error {
// This entry is persistent and the agent will make a best effort to // This entry is persistent and the agent will make a best effort to
// ensure it is registered. The Check may include a CheckType which // ensure it is registered. The Check may include a CheckType which
// is used to automatically update the check status // is used to automatically update the check status
func (a *Agent) AddCheck(check *structs.HealthCheck, chkType *CheckType, persist bool) error { func (a *Agent) AddCheck(check *structs.HealthCheck, chkType *CheckType, persist bool, token string) error {
if check.CheckID == "" { if check.CheckID == "" {
return fmt.Errorf("CheckID missing") return fmt.Errorf("CheckID missing")
} }
@ -783,7 +783,7 @@ func (a *Agent) AddCheck(check *structs.HealthCheck, chkType *CheckType, persist
} }
// Add to the local state for anti-entropy // Add to the local state for anti-entropy
a.state.AddCheck(check) a.state.AddCheck(check, token)
// Persist the check // Persist the check
if persist { if persist {
@ -928,8 +928,7 @@ func (a *Agent) loadServices(conf *Config) error {
for _, service := range conf.Services { for _, service := range conf.Services {
ns := service.NodeService() ns := service.NodeService()
chkTypes := service.CheckTypes() chkTypes := service.CheckTypes()
a.state.SetServiceToken(service.ID, service.Token) if err := a.AddService(ns, chkTypes, false, service.Token); err != nil {
if err := a.AddService(ns, chkTypes, false); err != nil {
return fmt.Errorf("Failed to register service '%s': %v", service.ID, err) return fmt.Errorf("Failed to register service '%s': %v", service.ID, err)
} }
} }
@ -958,6 +957,7 @@ func (a *Agent) loadServices(conf *Config) error {
} }
var wrapped *persistedService var wrapped *persistedService
var token string
var svc *structs.NodeService var svc *structs.NodeService
if err := json.Unmarshal(content, &wrapped); err != nil { if err := json.Unmarshal(content, &wrapped); err != nil {
// Backwards-compatibility for pre-0.5.1 persisted services // Backwards-compatibility for pre-0.5.1 persisted services
@ -966,7 +966,7 @@ func (a *Agent) loadServices(conf *Config) error {
} }
} else { } else {
svc = wrapped.Service svc = wrapped.Service
a.state.SetServiceToken(svc.ID, wrapped.Token) token = wrapped.Token
} }
if _, ok := a.state.services[svc.ID]; ok { if _, ok := a.state.services[svc.ID]; ok {
@ -978,7 +978,7 @@ func (a *Agent) loadServices(conf *Config) error {
} else { } else {
a.logger.Printf("[DEBUG] agent: restored service definition %q from %q", a.logger.Printf("[DEBUG] agent: restored service definition %q from %q",
svc.ID, filePath) svc.ID, filePath)
return a.AddService(svc, nil, false) return a.AddService(svc, nil, false, token)
} }
}) })
@ -1007,8 +1007,7 @@ func (a *Agent) loadChecks(conf *Config) error {
for _, check := range conf.Checks { for _, check := range conf.Checks {
health := check.HealthCheck(conf.NodeName) health := check.HealthCheck(conf.NodeName)
chkType := &check.CheckType chkType := &check.CheckType
a.state.SetCheckToken(check.ID, check.Token) if err := a.AddCheck(health, chkType, false, check.Token); err != nil {
if err := a.AddCheck(health, chkType, false); err != nil {
return fmt.Errorf("Failed to register check '%s': %v %v", check.Name, err, check) return fmt.Errorf("Failed to register check '%s': %v %v", check.Name, err, check)
} }
} }
@ -1040,7 +1039,6 @@ func (a *Agent) loadChecks(conf *Config) error {
if err := json.Unmarshal(content, &p); err != nil { if err := json.Unmarshal(content, &p); err != nil {
return err return err
} }
a.state.SetCheckToken(p.Check.CheckID, p.Token)
if _, ok := a.state.checks[p.Check.CheckID]; ok { if _, ok := a.state.checks[p.Check.CheckID]; ok {
// Purge previously persisted check. This allows config to be // Purge previously persisted check. This allows config to be
@ -1053,7 +1051,7 @@ func (a *Agent) loadChecks(conf *Config) error {
// services into the active pool // services into the active pool
p.Check.Status = structs.HealthCritical p.Check.Status = structs.HealthCritical
if err := a.AddCheck(p.Check, p.ChkType, false); err != nil { if err := a.AddCheck(p.Check, p.ChkType, false, p.Token); err != nil {
// Purge the check if it is unable to be restored. // Purge the check if it is unable to be restored.
a.logger.Printf("[WARN] agent: Failed to restore check %q: %s", a.logger.Printf("[WARN] agent: Failed to restore check %q: %s",
p.Check.CheckID, err) p.Check.CheckID, err)
@ -1130,7 +1128,7 @@ func (a *Agent) EnableServiceMaintenance(serviceID, reason string) error {
ServiceName: service.Service, ServiceName: service.Service,
Status: structs.HealthCritical, Status: structs.HealthCritical,
} }
a.AddCheck(check, nil, true) a.AddCheck(check, nil, true, "")
a.logger.Printf("[INFO] agent: Service %q entered maintenance mode", serviceID) a.logger.Printf("[INFO] agent: Service %q entered maintenance mode", serviceID)
return nil return nil
@ -1176,7 +1174,7 @@ func (a *Agent) EnableNodeMaintenance(reason string) {
Notes: reason, Notes: reason,
Status: structs.HealthCritical, Status: structs.HealthCritical,
} }
a.AddCheck(check, nil, true) a.AddCheck(check, nil, true, "")
a.logger.Printf("[INFO] agent: Node entered maintenance mode") a.logger.Printf("[INFO] agent: Node entered maintenance mode")
} }

View File

@ -100,10 +100,9 @@ func (s *HTTPServer) AgentRegisterCheck(resp http.ResponseWriter, req *http.Requ
// Get the provided token, if any // Get the provided token, if any
var token string var token string
s.parseToken(req, &token) s.parseToken(req, &token)
s.agent.state.SetCheckToken(health.CheckID, token)
// Add the check // Add the check
if err := s.agent.AddCheck(health, chkType, true); err != nil { if err := s.agent.AddCheck(health, chkType, true, token); err != nil {
return nil, err return nil, err
} }
s.syncChanges() s.syncChanges()
@ -207,10 +206,9 @@ func (s *HTTPServer) AgentRegisterService(resp http.ResponseWriter, req *http.Re
// Get the provided token, if any // Get the provided token, if any
var token string var token string
s.parseToken(req, &token) s.parseToken(req, &token)
s.agent.state.SetServiceToken(ns.ID, token)
// Add the check // Add the check
if err := s.agent.AddService(ns, chkTypes, true); err != nil { if err := s.agent.AddService(ns, chkTypes, true, token); err != nil {
return nil, err return nil, err
} }
s.syncChanges() s.syncChanges()

View File

@ -25,7 +25,7 @@ func TestHTTPAgentServices(t *testing.T) {
Tags: []string{"master"}, Tags: []string{"master"},
Port: 5000, Port: 5000,
} }
srv.agent.state.AddService(srv1) srv.agent.state.AddService(srv1, "")
obj, err := srv.AgentServices(nil, nil) obj, err := srv.AgentServices(nil, nil)
if err != nil { if err != nil {
@ -52,7 +52,7 @@ func TestHTTPAgentChecks(t *testing.T) {
Name: "mysql", Name: "mysql",
Status: structs.HealthPassing, Status: structs.HealthPassing,
} }
srv.agent.state.AddCheck(chk1) srv.agent.state.AddCheck(chk1, "")
obj, err := srv.AgentChecks(nil, nil) obj, err := srv.AgentChecks(nil, nil)
if err != nil { if err != nil {
@ -294,7 +294,7 @@ func TestHTTPAgentDeregisterCheck(t *testing.T) {
defer srv.agent.Shutdown() defer srv.agent.Shutdown()
chk := &structs.HealthCheck{Name: "test", CheckID: "test"} chk := &structs.HealthCheck{Name: "test", CheckID: "test"}
if err := srv.agent.AddCheck(chk, nil, false); err != nil { if err := srv.agent.AddCheck(chk, nil, false, ""); err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
@ -326,7 +326,7 @@ func TestHTTPAgentPassCheck(t *testing.T) {
chk := &structs.HealthCheck{Name: "test", CheckID: "test"} chk := &structs.HealthCheck{Name: "test", CheckID: "test"}
chkType := &CheckType{TTL: 15 * time.Second} chkType := &CheckType{TTL: 15 * time.Second}
if err := srv.agent.AddCheck(chk, chkType, false); err != nil { if err := srv.agent.AddCheck(chk, chkType, false, ""); err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
@ -359,7 +359,7 @@ func TestHTTPAgentWarnCheck(t *testing.T) {
chk := &structs.HealthCheck{Name: "test", CheckID: "test"} chk := &structs.HealthCheck{Name: "test", CheckID: "test"}
chkType := &CheckType{TTL: 15 * time.Second} chkType := &CheckType{TTL: 15 * time.Second}
if err := srv.agent.AddCheck(chk, chkType, false); err != nil { if err := srv.agent.AddCheck(chk, chkType, false, ""); err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
@ -392,7 +392,7 @@ func TestHTTPAgentFailCheck(t *testing.T) {
chk := &structs.HealthCheck{Name: "test", CheckID: "test"} chk := &structs.HealthCheck{Name: "test", CheckID: "test"}
chkType := &CheckType{TTL: 15 * time.Second} chkType := &CheckType{TTL: 15 * time.Second}
if err := srv.agent.AddCheck(chk, chkType, false); err != nil { if err := srv.agent.AddCheck(chk, chkType, false, ""); err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
@ -485,7 +485,7 @@ func TestHTTPAgentDeregisterService(t *testing.T) {
ID: "test", ID: "test",
Service: "test", Service: "test",
} }
if err := srv.agent.AddService(service, nil, false); err != nil { if err := srv.agent.AddService(service, nil, false, ""); err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
@ -571,7 +571,7 @@ func TestHTTPAgent_EnableServiceMaintenance(t *testing.T) {
ID: "test", ID: "test",
Service: "test", Service: "test",
} }
if err := srv.agent.AddService(service, nil, false); err != nil { if err := srv.agent.AddService(service, nil, false, ""); err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
@ -609,7 +609,7 @@ func TestHTTPAgent_DisableServiceMaintenance(t *testing.T) {
ID: "test", ID: "test",
Service: "test", Service: "test",
} }
if err := srv.agent.AddService(service, nil, false); err != nil { if err := srv.agent.AddService(service, nil, false, ""); err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }

View File

@ -153,7 +153,7 @@ func TestAgent_AddService(t *testing.T) {
Notes: "redis heath check 2", Notes: "redis heath check 2",
}, },
} }
err := agent.AddService(srv, chkTypes, false) err := agent.AddService(srv, chkTypes, false, "")
if err != nil { if err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
@ -197,7 +197,7 @@ func TestAgent_AddService(t *testing.T) {
Notes: "memcache heath check 2", Notes: "memcache heath check 2",
}, },
} }
if err := agent.AddService(srv, chkTypes, false); err != nil { if err := agent.AddService(srv, chkTypes, false, ""); err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
@ -261,7 +261,7 @@ func TestAgent_RemoveService(t *testing.T) {
} }
chkTypes := CheckTypes{&CheckType{TTL: time.Minute}} chkTypes := CheckTypes{&CheckType{TTL: time.Minute}}
if err := agent.AddService(srv, chkTypes, false); err != nil { if err := agent.AddService(srv, chkTypes, false, ""); err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
@ -284,7 +284,7 @@ func TestAgent_RemoveService(t *testing.T) {
&CheckType{TTL: time.Minute}, &CheckType{TTL: time.Minute},
&CheckType{TTL: 30 * time.Second}, &CheckType{TTL: 30 * time.Second},
} }
if err := agent.AddService(srv, chkTypes, false); err != nil { if err := agent.AddService(srv, chkTypes, false, ""); err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
@ -331,7 +331,7 @@ func TestAgent_AddCheck(t *testing.T) {
Script: "exit 0", Script: "exit 0",
Interval: 15 * time.Second, Interval: 15 * time.Second,
} }
err := agent.AddCheck(health, chk, false) err := agent.AddCheck(health, chk, false, "")
if err != nil { if err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
@ -362,7 +362,7 @@ func TestAgent_AddCheck_MinInterval(t *testing.T) {
Script: "exit 0", Script: "exit 0",
Interval: time.Microsecond, Interval: time.Microsecond,
} }
err := agent.AddCheck(health, chk, false) err := agent.AddCheck(health, chk, false, "")
if err != nil { if err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
@ -395,7 +395,7 @@ func TestAgent_AddCheck_MissingService(t *testing.T) {
Script: "exit 0", Script: "exit 0",
Interval: time.Microsecond, Interval: time.Microsecond,
} }
err := agent.AddCheck(health, chk, false) err := agent.AddCheck(health, chk, false, "")
if err == nil || err.Error() != `ServiceID "baz" does not exist` { if err == nil || err.Error() != `ServiceID "baz" does not exist` {
t.Fatalf("expected service id error, got: %v", err) t.Fatalf("expected service id error, got: %v", err)
} }
@ -426,7 +426,7 @@ func TestAgent_RemoveCheck(t *testing.T) {
Script: "exit 0", Script: "exit 0",
Interval: 15 * time.Second, Interval: 15 * time.Second,
} }
err := agent.AddCheck(health, chk, false) err := agent.AddCheck(health, chk, false, "")
if err != nil { if err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
@ -461,7 +461,7 @@ func TestAgent_UpdateCheck(t *testing.T) {
chk := &CheckType{ chk := &CheckType{
TTL: 15 * time.Second, TTL: 15 * time.Second,
} }
err := agent.AddCheck(health, chk, false) err := agent.AddCheck(health, chk, false, "")
if err != nil { if err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
@ -521,11 +521,8 @@ func TestAgent_PersistService(t *testing.T) {
file := filepath.Join(agent.config.DataDir, servicesDir, stringHash(svc.ID)) file := filepath.Join(agent.config.DataDir, servicesDir, stringHash(svc.ID))
// Configure a service token
agent.state.SetServiceToken(svc.ID, "hello")
// Check is not persisted unless requested // Check is not persisted unless requested
if err := agent.AddService(svc, nil, false); err != nil { if err := agent.AddService(svc, nil, false, ""); err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
if _, err := os.Stat(file); err == nil { if _, err := os.Stat(file); err == nil {
@ -533,7 +530,7 @@ func TestAgent_PersistService(t *testing.T) {
} }
// Persists to file if requested // Persists to file if requested
if err := agent.AddService(svc, nil, true); err != nil { if err := agent.AddService(svc, nil, true, "mytoken"); err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
@ -542,7 +539,7 @@ func TestAgent_PersistService(t *testing.T) {
} }
expected, err := json.Marshal(persistedService{ expected, err := json.Marshal(persistedService{
Token: "hello", Token: "mytoken",
Service: svc, Service: svc,
}) })
if err != nil { if err != nil {
@ -567,7 +564,7 @@ func TestAgent_PersistService(t *testing.T) {
if _, ok := agent2.state.services[svc.ID]; !ok { if _, ok := agent2.state.services[svc.ID]; !ok {
t.Fatalf("bad: %#v", agent2.state.services) t.Fatalf("bad: %#v", agent2.state.services)
} }
if agent2.state.serviceTokens[svc.ID] != "hello" { if agent2.state.serviceTokens[svc.ID] != "mytoken" {
t.Fatalf("bad: %#v", agent2.state.services[svc.ID]) t.Fatalf("bad: %#v", agent2.state.services[svc.ID])
} }
} }
@ -632,7 +629,7 @@ func TestAgent_PurgeService(t *testing.T) {
} }
file := filepath.Join(agent.config.DataDir, servicesDir, stringHash(svc.ID)) file := filepath.Join(agent.config.DataDir, servicesDir, stringHash(svc.ID))
if err := agent.AddService(svc, nil, true); err != nil { if err := agent.AddService(svc, nil, true, ""); err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
@ -668,7 +665,7 @@ func TestAgent_PurgeServiceOnDuplicate(t *testing.T) {
} }
// First persist the service // First persist the service
if err := agent.AddService(svc1, nil, true); err != nil { if err := agent.AddService(svc1, nil, true, ""); err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
agent.Shutdown() agent.Shutdown()
@ -722,11 +719,8 @@ func TestAgent_PersistCheck(t *testing.T) {
file := filepath.Join(agent.config.DataDir, checksDir, stringHash(check.CheckID)) file := filepath.Join(agent.config.DataDir, checksDir, stringHash(check.CheckID))
// Configure a service registration token
agent.state.SetCheckToken(check.CheckID, "hello")
// Not persisted if not requested // Not persisted if not requested
if err := agent.AddCheck(check, chkType, false); err != nil { if err := agent.AddCheck(check, chkType, false, ""); err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
if _, err := os.Stat(file); err == nil { if _, err := os.Stat(file); err == nil {
@ -734,7 +728,7 @@ func TestAgent_PersistCheck(t *testing.T) {
} }
// Should persist if requested // Should persist if requested
if err := agent.AddCheck(check, chkType, true); err != nil { if err := agent.AddCheck(check, chkType, true, "mytoken"); err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
@ -745,7 +739,7 @@ func TestAgent_PersistCheck(t *testing.T) {
expected, err := json.Marshal(persistedCheck{ expected, err := json.Marshal(persistedCheck{
Check: check, Check: check,
ChkType: chkType, ChkType: chkType,
Token: "hello", Token: "mytoken",
}) })
if err != nil { if err != nil {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
@ -778,7 +772,7 @@ func TestAgent_PersistCheck(t *testing.T) {
if _, ok := agent2.checkMonitors[check.CheckID]; !ok { if _, ok := agent2.checkMonitors[check.CheckID]; !ok {
t.Fatalf("bad: %#v", agent2.checkMonitors) t.Fatalf("bad: %#v", agent2.checkMonitors)
} }
if agent2.state.checkTokens[check.CheckID] != "hello" { if agent2.state.checkTokens[check.CheckID] != "mytoken" {
t.Fatalf("bad: %s", agent2.state.checkTokens[check.CheckID]) t.Fatalf("bad: %s", agent2.state.checkTokens[check.CheckID])
} }
} }
@ -797,7 +791,7 @@ func TestAgent_PurgeCheck(t *testing.T) {
} }
file := filepath.Join(agent.config.DataDir, checksDir, stringHash(check.CheckID)) file := filepath.Join(agent.config.DataDir, checksDir, stringHash(check.CheckID))
if err := agent.AddCheck(check, nil, true); err != nil { if err := agent.AddCheck(check, nil, true, ""); err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
@ -833,7 +827,7 @@ func TestAgent_PurgeCheckOnDuplicate(t *testing.T) {
} }
// First persist the check // First persist the check
if err := agent.AddCheck(check1, nil, true); err != nil { if err := agent.AddCheck(check1, nil, true, ""); err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
agent.Shutdown() agent.Shutdown()
@ -906,7 +900,7 @@ func TestAgent_unloadChecks(t *testing.T) {
Tags: []string{"foo"}, Tags: []string{"foo"},
Port: 8000, Port: 8000,
} }
if err := agent.AddService(svc, nil, false); err != nil { if err := agent.AddService(svc, nil, false, ""); err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
@ -919,7 +913,7 @@ func TestAgent_unloadChecks(t *testing.T) {
ServiceID: "redis", ServiceID: "redis",
ServiceName: "redis", ServiceName: "redis",
} }
if err := agent.AddCheck(check1, nil, false); err != nil { if err := agent.AddCheck(check1, nil, false, ""); err != nil {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }
found := false found := false
@ -981,7 +975,7 @@ func TestAgent_unloadServices(t *testing.T) {
} }
// Register the service // Register the service
if err := agent.AddService(svc, nil, false); err != nil { if err := agent.AddService(svc, nil, false, ""); err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
found := false found := false
@ -1029,7 +1023,7 @@ func TestAgent_ServiceMaintenanceMode(t *testing.T) {
} }
// Register the service // Register the service
if err := agent.AddService(svc, nil, false); err != nil { if err := agent.AddService(svc, nil, false, ""); err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
@ -1129,7 +1123,7 @@ func TestAgent_checkStateSnapshot(t *testing.T) {
Tags: []string{"foo"}, Tags: []string{"foo"},
Port: 8000, Port: 8000,
} }
if err := agent.AddService(svc, nil, false); err != nil { if err := agent.AddService(svc, nil, false, ""); err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
@ -1142,7 +1136,7 @@ func TestAgent_checkStateSnapshot(t *testing.T) {
ServiceID: "redis", ServiceID: "redis",
ServiceName: "redis", ServiceName: "redis",
} }
if err := agent.AddCheck(check1, nil, true); err != nil { if err := agent.AddCheck(check1, nil, true, ""); err != nil {
t.Fatalf("err: %s", err) t.Fatalf("err: %s", err)
} }

View File

@ -123,23 +123,6 @@ func (l *localState) isPaused() bool {
return atomic.LoadInt32(&l.paused) == 1 return atomic.LoadInt32(&l.paused) == 1
} }
// SetServiceToken configures the provided token for the service ID.
// The token will be used to perform service registration operations.
func (l *localState) SetServiceToken(id, token string) {
if token != "" {
l.Lock()
defer l.Unlock()
l.serviceTokens[id] = token
}
}
// RemoveServiceToken is used to remove a configured service token.
func (l *localState) RemoveServiceToken(id string) {
l.Lock()
defer l.Unlock()
delete(l.serviceTokens, id)
}
// ServiceToken returns the configured ACL token for the given // ServiceToken returns the configured ACL token for the given
// service ID. If none is present, the agent's token is returned. // service ID. If none is present, the agent's token is returned.
func (l *localState) ServiceToken(id string) string { func (l *localState) ServiceToken(id string) string {
@ -160,7 +143,7 @@ func (l *localState) serviceToken(id string) string {
// AddService is used to add a service entry to the local state. // AddService is used to add a service entry to the local state.
// This entry is persistent and the agent will make a best effort to // This entry is persistent and the agent will make a best effort to
// ensure it is registered // ensure it is registered
func (l *localState) AddService(service *structs.NodeService) { func (l *localState) AddService(service *structs.NodeService, token string) {
// Assign the ID if none given // Assign the ID if none given
if service.ID == "" && service.Service != "" { if service.ID == "" && service.Service != "" {
service.ID = service.Service service.ID = service.Service
@ -171,6 +154,7 @@ func (l *localState) AddService(service *structs.NodeService) {
l.services[service.ID] = service l.services[service.ID] = service
l.serviceStatus[service.ID] = syncStatus{} l.serviceStatus[service.ID] = syncStatus{}
l.serviceTokens[service.ID] = token
l.changeMade() l.changeMade()
} }
@ -181,6 +165,7 @@ func (l *localState) RemoveService(serviceID string) {
defer l.Unlock() defer l.Unlock()
delete(l.services, serviceID) delete(l.services, serviceID)
delete(l.serviceTokens, serviceID)
l.serviceStatus[serviceID] = syncStatus{remoteDelete: true} l.serviceStatus[serviceID] = syncStatus{remoteDelete: true}
l.changeMade() l.changeMade()
} }
@ -198,23 +183,6 @@ func (l *localState) Services() map[string]*structs.NodeService {
return services return services
} }
// SetCheckToken is used to configure an ACL token for a specific
// health check. The token is used during check registration operations.
func (l *localState) SetCheckToken(id, token string) {
if token != "" {
l.Lock()
defer l.Unlock()
l.checkTokens[id] = token
}
}
// RemoveCheckToken is used to remove a configured check token.
func (l *localState) RemoveCheckToken(id string) {
l.Lock()
defer l.Unlock()
delete(l.checkTokens, id)
}
// CheckToken is used to return the configured health check token, or // CheckToken is used to return the configured health check token, or
// if none is configured, the default agent ACL token. // if none is configured, the default agent ACL token.
func (l *localState) CheckToken(id string) string { func (l *localState) CheckToken(id string) string {
@ -235,7 +203,7 @@ func (l *localState) checkToken(id string) string {
// AddCheck is used to add a health check to the local state. // AddCheck is used to add a health check to the local state.
// This entry is persistent and the agent will make a best effort to // This entry is persistent and the agent will make a best effort to
// ensure it is registered // ensure it is registered
func (l *localState) AddCheck(check *structs.HealthCheck) { func (l *localState) AddCheck(check *structs.HealthCheck, token string) {
// Set the node name // Set the node name
check.Node = l.config.NodeName check.Node = l.config.NodeName
@ -244,6 +212,7 @@ func (l *localState) AddCheck(check *structs.HealthCheck) {
l.checks[check.CheckID] = check l.checks[check.CheckID] = check
l.checkStatus[check.CheckID] = syncStatus{} l.checkStatus[check.CheckID] = syncStatus{}
l.checkTokens[check.CheckID] = token
l.changeMade() l.changeMade()
} }
@ -254,6 +223,7 @@ func (l *localState) RemoveCheck(checkID string) {
defer l.Unlock() defer l.Unlock()
delete(l.checks, checkID) delete(l.checks, checkID)
delete(l.checkTokens, checkID)
l.checkStatus[checkID] = syncStatus{remoteDelete: true} l.checkStatus[checkID] = syncStatus{remoteDelete: true}
l.changeMade() l.changeMade()
} }

View File

@ -34,7 +34,7 @@ func TestAgentAntiEntropy_Services(t *testing.T) {
Tags: []string{"master"}, Tags: []string{"master"},
Port: 5000, Port: 5000,
} }
agent.state.AddService(srv1) agent.state.AddService(srv1, "")
args.Service = srv1 args.Service = srv1
if err := agent.RPC("Catalog.Register", args, &out); err != nil { if err := agent.RPC("Catalog.Register", args, &out); err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
@ -47,7 +47,7 @@ func TestAgentAntiEntropy_Services(t *testing.T) {
Tags: []string{}, Tags: []string{},
Port: 8000, Port: 8000,
} }
agent.state.AddService(srv2) agent.state.AddService(srv2, "")
srv2_mod := new(structs.NodeService) srv2_mod := new(structs.NodeService)
*srv2_mod = *srv2 *srv2_mod = *srv2
@ -64,7 +64,7 @@ func TestAgentAntiEntropy_Services(t *testing.T) {
Tags: []string{}, Tags: []string{},
Port: 80, Port: 80,
} }
agent.state.AddService(srv3) agent.state.AddService(srv3, "")
// Exists remote (delete) // Exists remote (delete)
srv4 := &structs.NodeService{ srv4 := &structs.NodeService{
@ -86,7 +86,7 @@ func TestAgentAntiEntropy_Services(t *testing.T) {
Address: "127.0.0.10", Address: "127.0.0.10",
Port: 8000, Port: 8000,
} }
agent.state.AddService(srv5) agent.state.AddService(srv5, "")
// Exists local, in sync, remote missing (create) // Exists local, in sync, remote missing (create)
srv6 := &structs.NodeService{ srv6 := &structs.NodeService{
@ -95,7 +95,7 @@ func TestAgentAntiEntropy_Services(t *testing.T) {
Tags: []string{}, Tags: []string{},
Port: 11211, Port: 11211,
} }
agent.state.AddService(srv6) agent.state.AddService(srv6, "")
agent.state.serviceStatus["cache"] = syncStatus{inSync: true} agent.state.serviceStatus["cache"] = syncStatus{inSync: true}
srv5_mod := new(structs.NodeService) srv5_mod := new(structs.NodeService)
@ -185,7 +185,7 @@ func TestAgentAntiEntropy_Services_WithChecks(t *testing.T) {
Tags: []string{"master"}, Tags: []string{"master"},
Port: 5000, Port: 5000,
} }
agent.state.AddService(srv) agent.state.AddService(srv, "")
chk := &structs.HealthCheck{ chk := &structs.HealthCheck{
Node: agent.config.NodeName, Node: agent.config.NodeName,
@ -194,7 +194,7 @@ func TestAgentAntiEntropy_Services_WithChecks(t *testing.T) {
ServiceID: "mysql", ServiceID: "mysql",
Status: structs.HealthPassing, Status: structs.HealthPassing,
} }
agent.state.AddCheck(chk) agent.state.AddCheck(chk, "")
// Sync the service once // Sync the service once
if err := agent.state.syncService("mysql"); err != nil { if err := agent.state.syncService("mysql"); err != nil {
@ -236,7 +236,7 @@ func TestAgentAntiEntropy_Services_WithChecks(t *testing.T) {
Tags: []string{"master"}, Tags: []string{"master"},
Port: 5000, Port: 5000,
} }
agent.state.AddService(srv) agent.state.AddService(srv, "")
chk1 := &structs.HealthCheck{ chk1 := &structs.HealthCheck{
Node: agent.config.NodeName, Node: agent.config.NodeName,
@ -245,7 +245,7 @@ func TestAgentAntiEntropy_Services_WithChecks(t *testing.T) {
ServiceID: "redis", ServiceID: "redis",
Status: structs.HealthPassing, Status: structs.HealthPassing,
} }
agent.state.AddCheck(chk1) agent.state.AddCheck(chk1, "")
chk2 := &structs.HealthCheck{ chk2 := &structs.HealthCheck{
Node: agent.config.NodeName, Node: agent.config.NodeName,
@ -254,7 +254,7 @@ func TestAgentAntiEntropy_Services_WithChecks(t *testing.T) {
ServiceID: "redis", ServiceID: "redis",
Status: structs.HealthPassing, Status: structs.HealthPassing,
} }
agent.state.AddCheck(chk2) agent.state.AddCheck(chk2, "")
// Sync the service once // Sync the service once
if err := agent.state.syncService("redis"); err != nil { if err := agent.state.syncService("redis"); err != nil {
@ -326,7 +326,7 @@ func TestAgentAntiEntropy_Services_ACLDeny(t *testing.T) {
Tags: []string{"master"}, Tags: []string{"master"},
Port: 5000, Port: 5000,
} }
agent.state.AddService(srv1) agent.state.AddService(srv1, "")
// Create service (Disallowed) // Create service (Disallowed)
srv2 := &structs.NodeService{ srv2 := &structs.NodeService{
@ -335,7 +335,7 @@ func TestAgentAntiEntropy_Services_ACLDeny(t *testing.T) {
Tags: []string{"foo"}, Tags: []string{"foo"},
Port: 5001, Port: 5001,
} }
agent.state.AddService(srv2) agent.state.AddService(srv2, "")
// Trigger anti-entropy run and wait // Trigger anti-entropy run and wait
agent.StartSync() agent.StartSync()
@ -409,7 +409,7 @@ func TestAgentAntiEntropy_Checks(t *testing.T) {
Name: "mysql", Name: "mysql",
Status: structs.HealthPassing, Status: structs.HealthPassing,
} }
agent.state.AddCheck(chk1) agent.state.AddCheck(chk1, "")
args.Check = chk1 args.Check = chk1
if err := agent.RPC("Catalog.Register", args, &out); err != nil { if err := agent.RPC("Catalog.Register", args, &out); err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
@ -422,7 +422,7 @@ func TestAgentAntiEntropy_Checks(t *testing.T) {
Name: "redis", Name: "redis",
Status: structs.HealthPassing, Status: structs.HealthPassing,
} }
agent.state.AddCheck(chk2) agent.state.AddCheck(chk2, "")
chk2_mod := new(structs.HealthCheck) chk2_mod := new(structs.HealthCheck)
*chk2_mod = *chk2 *chk2_mod = *chk2
@ -439,7 +439,7 @@ func TestAgentAntiEntropy_Checks(t *testing.T) {
Name: "web", Name: "web",
Status: structs.HealthPassing, Status: structs.HealthPassing,
} }
agent.state.AddCheck(chk3) agent.state.AddCheck(chk3, "")
// Exists remote (delete) // Exists remote (delete)
chk4 := &structs.HealthCheck{ chk4 := &structs.HealthCheck{
@ -460,7 +460,7 @@ func TestAgentAntiEntropy_Checks(t *testing.T) {
Name: "cache", Name: "cache",
Status: structs.HealthPassing, Status: structs.HealthPassing,
} }
agent.state.AddCheck(chk5) agent.state.AddCheck(chk5, "")
agent.state.checkStatus["cache"] = syncStatus{inSync: true} agent.state.checkStatus["cache"] = syncStatus{inSync: true}
// Trigger anti-entropy run and wait // Trigger anti-entropy run and wait
@ -539,7 +539,7 @@ func TestAgentAntiEntropy_Check_DeferSync(t *testing.T) {
Status: structs.HealthPassing, Status: structs.HealthPassing,
Output: "", Output: "",
} }
agent.state.AddCheck(check) agent.state.AddCheck(check, "")
// Trigger anti-entropy run and wait // Trigger anti-entropy run and wait
agent.StartSync() agent.StartSync()
@ -627,13 +627,13 @@ func TestAgent_serviceTokens(t *testing.T) {
} }
// Returns configured token // Returns configured token
l.SetServiceToken("redis", "abc123") l.serviceTokens["redis"] = "abc123"
if token := l.ServiceToken("redis"); token != "abc123" { if token := l.ServiceToken("redis"); token != "abc123" {
t.Fatalf("bad: %s", token) t.Fatalf("bad: %s", token)
} }
// Removes token // Removes token
l.RemoveServiceToken("redis") l.RemoveService("redis")
if token := l.ServiceToken("redis"); token != "default" { if token := l.ServiceToken("redis"); token != "default" {
t.Fatalf("bad: %s", token) t.Fatalf("bad: %s", token)
} }
@ -651,13 +651,13 @@ func TestAgent_checkTokens(t *testing.T) {
} }
// Returns configured token // Returns configured token
l.SetCheckToken("mem", "abc123") l.checkTokens["mem"] = "abc123"
if token := l.CheckToken("mem"); token != "abc123" { if token := l.CheckToken("mem"); token != "abc123" {
t.Fatalf("bad: %s", token) t.Fatalf("bad: %s", token)
} }
// Removes token // Removes token
l.RemoveCheckToken("mem") l.RemoveCheck("mem")
if token := l.CheckToken("mem"); token != "default" { if token := l.CheckToken("mem"); token != "default" {
t.Fatalf("bad: %s", token) t.Fatalf("bad: %s", token)
} }

View File

@ -57,7 +57,7 @@ func TestShouldProcessUserEvent(t *testing.T) {
Tags: []string{"test", "foo", "bar", "master"}, Tags: []string{"test", "foo", "bar", "master"},
Port: 5000, Port: 5000,
} }
agent.state.AddService(srv1) agent.state.AddService(srv1, "")
p := &UserEvent{} p := &UserEvent{}
if !agent.shouldProcessUserEvent(p) { if !agent.shouldProcessUserEvent(p) {
@ -159,7 +159,7 @@ func TestFireReceiveEvent(t *testing.T) {
Tags: []string{"test", "foo", "bar", "master"}, Tags: []string{"test", "foo", "bar", "master"},
Port: 5000, Port: 5000,
} }
agent.state.AddService(srv1) agent.state.AddService(srv1, "")
p1 := &UserEvent{Name: "deploy", ServiceFilter: "web"} p1 := &UserEvent{Name: "deploy", ServiceFilter: "web"}
err := agent.UserEvent("", p1) err := agent.UserEvent("", p1)

View File

@ -42,7 +42,7 @@ func TestMaintCommandRun_NoArgs(t *testing.T) {
ID: "test", ID: "test",
Service: "test", Service: "test",
} }
if err := a1.agent.AddService(service, nil, false); err != nil { if err := a1.agent.AddService(service, nil, false, ""); err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
if err := a1.agent.EnableServiceMaintenance("test", "broken 1"); err != nil { if err := a1.agent.EnableServiceMaintenance("test", "broken 1"); err != nil {
@ -132,7 +132,7 @@ func TestMaintCommandRun_EnableServiceMaintenance(t *testing.T) {
ID: "test", ID: "test",
Service: "test", Service: "test",
} }
if err := a1.agent.AddService(service, nil, false); err != nil { if err := a1.agent.AddService(service, nil, false, ""); err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
@ -164,7 +164,7 @@ func TestMaintCommandRun_DisableServiceMaintenance(t *testing.T) {
ID: "test", ID: "test",
Service: "test", Service: "test",
} }
if err := a1.agent.AddService(service, nil, false); err != nil { if err := a1.agent.AddService(service, nil, false, ""); err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }