2015-09-07 02:08:05 +00:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
2015-09-11 22:19:26 +00:00
|
|
|
"net"
|
2015-09-07 02:08:05 +00:00
|
|
|
"net/http"
|
2017-09-14 17:04:48 +00:00
|
|
|
"sort"
|
2016-10-17 17:48:04 +00:00
|
|
|
"strings"
|
2015-09-07 02:08:05 +00:00
|
|
|
|
2017-10-09 23:09:44 +00:00
|
|
|
"github.com/hashicorp/nomad/acl"
|
2016-10-17 17:48:04 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
2015-09-07 02:08:05 +00:00
|
|
|
"github.com/hashicorp/serf/serf"
|
2017-08-08 04:33:18 +00:00
|
|
|
"github.com/mitchellh/copystructure"
|
2015-09-07 02:08:05 +00:00
|
|
|
)
|
|
|
|
|
2015-09-11 22:19:26 +00:00
|
|
|
type Member struct {
|
|
|
|
Name string
|
|
|
|
Addr net.IP
|
|
|
|
Port uint16
|
|
|
|
Tags map[string]string
|
|
|
|
Status string
|
|
|
|
ProtocolMin uint8
|
|
|
|
ProtocolMax uint8
|
|
|
|
ProtocolCur uint8
|
|
|
|
DelegateMin uint8
|
|
|
|
DelegateMax uint8
|
|
|
|
DelegateCur uint8
|
|
|
|
}
|
|
|
|
|
|
|
|
func nomadMember(m serf.Member) Member {
|
|
|
|
return Member{
|
|
|
|
Name: m.Name,
|
|
|
|
Addr: m.Addr,
|
|
|
|
Port: m.Port,
|
|
|
|
Tags: m.Tags,
|
|
|
|
Status: m.Status.String(),
|
|
|
|
ProtocolMin: m.ProtocolMin,
|
|
|
|
ProtocolMax: m.ProtocolMax,
|
|
|
|
ProtocolCur: m.ProtocolCur,
|
|
|
|
DelegateMin: m.DelegateMin,
|
|
|
|
DelegateMax: m.DelegateMax,
|
|
|
|
DelegateCur: m.DelegateCur,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-07 02:08:05 +00:00
|
|
|
func (s *HTTPServer) AgentSelfRequest(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
|
|
|
if req.Method != "GET" {
|
|
|
|
return nil, CodedError(405, ErrInvalidMethod)
|
|
|
|
}
|
|
|
|
|
2017-10-06 19:02:53 +00:00
|
|
|
var secret string
|
|
|
|
s.parseToken(req, &secret)
|
|
|
|
|
2017-10-09 23:09:44 +00:00
|
|
|
var aclObj *acl.ACL
|
|
|
|
var err error
|
2017-10-06 19:02:53 +00:00
|
|
|
|
2015-09-07 02:08:05 +00:00
|
|
|
// Get the member as a server
|
2015-09-11 22:19:26 +00:00
|
|
|
var member serf.Member
|
2017-10-09 23:09:44 +00:00
|
|
|
if srv := s.agent.Server(); srv != nil {
|
2015-09-11 22:19:26 +00:00
|
|
|
member = srv.LocalMember()
|
2017-10-09 23:09:44 +00:00
|
|
|
aclObj, err = srv.ResolveToken(secret)
|
|
|
|
} else {
|
|
|
|
// Not a Server; use the Client for token resolution
|
|
|
|
aclObj, err = s.agent.Client().ResolveToken(secret)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check agent read permissions
|
|
|
|
if aclObj != nil && !aclObj.AllowAgentRead() {
|
|
|
|
return nil, structs.ErrPermissionDenied
|
2015-09-07 02:08:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
self := agentSelf{
|
2015-09-11 22:19:26 +00:00
|
|
|
Member: nomadMember(member),
|
2015-09-07 02:08:05 +00:00
|
|
|
Stats: s.agent.Stats(),
|
|
|
|
}
|
2017-08-08 04:33:18 +00:00
|
|
|
if ac, err := copystructure.Copy(s.agent.config); err != nil {
|
|
|
|
return nil, CodedError(500, err.Error())
|
|
|
|
} else {
|
|
|
|
self.Config = ac.(*Config)
|
|
|
|
}
|
|
|
|
|
2017-08-08 17:54:11 +00:00
|
|
|
if self.Config != nil && self.Config.Vault != nil && self.Config.Vault.Token != "" {
|
2017-08-08 04:33:18 +00:00
|
|
|
self.Config.Vault.Token = "<redacted>"
|
|
|
|
}
|
|
|
|
|
2015-09-07 02:08:05 +00:00
|
|
|
return self, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *HTTPServer) AgentJoinRequest(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
|
|
|
if req.Method != "PUT" && req.Method != "POST" {
|
|
|
|
return nil, CodedError(405, ErrInvalidMethod)
|
|
|
|
}
|
|
|
|
srv := s.agent.Server()
|
|
|
|
if srv == nil {
|
|
|
|
return nil, CodedError(501, ErrInvalidMethod)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the join addresses
|
|
|
|
query := req.URL.Query()
|
|
|
|
addrs := query["address"]
|
|
|
|
if len(addrs) == 0 {
|
|
|
|
return nil, CodedError(400, "missing address to join")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attempt the join
|
|
|
|
num, err := srv.Join(addrs)
|
|
|
|
var errStr string
|
|
|
|
if err != nil {
|
|
|
|
errStr = err.Error()
|
|
|
|
}
|
|
|
|
return joinResult{num, errStr}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *HTTPServer) AgentMembersRequest(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
|
|
|
if req.Method != "GET" {
|
|
|
|
return nil, CodedError(405, ErrInvalidMethod)
|
|
|
|
}
|
2017-10-06 19:02:53 +00:00
|
|
|
|
|
|
|
var secret string
|
|
|
|
s.parseToken(req, &secret)
|
|
|
|
|
2017-10-09 23:09:44 +00:00
|
|
|
var aclObj *acl.ACL
|
|
|
|
var err error
|
|
|
|
|
|
|
|
if client := s.agent.Client(); client != nil {
|
|
|
|
aclObj, err = client.ResolveToken(secret)
|
|
|
|
} else {
|
|
|
|
aclObj, err = s.agent.Server().ResolveToken(secret)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
2017-10-06 19:02:53 +00:00
|
|
|
return nil, err
|
2017-10-09 23:09:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check node read permissions
|
|
|
|
if aclObj != nil && !aclObj.AllowNodeRead() {
|
2017-10-06 19:02:53 +00:00
|
|
|
return nil, structs.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
|
2016-11-03 21:14:52 +00:00
|
|
|
args := &structs.GenericRequest{}
|
|
|
|
var out structs.ServerMembersResponse
|
|
|
|
if err := s.agent.RPC("Status.Members", args, &out); err != nil {
|
|
|
|
return nil, err
|
2015-09-07 02:08:05 +00:00
|
|
|
}
|
2015-09-11 22:19:26 +00:00
|
|
|
|
2016-11-03 21:14:52 +00:00
|
|
|
return out, nil
|
2015-09-07 02:08:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *HTTPServer) AgentForceLeaveRequest(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
|
|
|
if req.Method != "PUT" && req.Method != "POST" {
|
|
|
|
return nil, CodedError(405, ErrInvalidMethod)
|
|
|
|
}
|
|
|
|
srv := s.agent.Server()
|
|
|
|
if srv == nil {
|
|
|
|
return nil, CodedError(501, ErrInvalidMethod)
|
|
|
|
}
|
|
|
|
|
2017-10-06 19:02:53 +00:00
|
|
|
var secret string
|
|
|
|
s.parseToken(req, &secret)
|
|
|
|
|
|
|
|
// Check agent write permissions
|
2017-10-09 22:49:04 +00:00
|
|
|
if aclObj, err := s.agent.Server().ResolveToken(secret); err != nil {
|
2017-10-06 19:02:53 +00:00
|
|
|
return nil, err
|
|
|
|
} else if aclObj != nil && !aclObj.AllowAgentWrite() {
|
|
|
|
return nil, structs.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
|
2015-09-07 02:08:05 +00:00
|
|
|
// Get the node to eject
|
|
|
|
node := req.URL.Query().Get("node")
|
|
|
|
if node == "" {
|
|
|
|
return nil, CodedError(400, "missing node to force leave")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attempt remove
|
|
|
|
err := srv.RemoveFailedNode(node)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-06-01 07:34:46 +00:00
|
|
|
// AgentServersRequest is used to query the list of servers used by the Nomad
|
|
|
|
// Client for RPCs. This endpoint can also be used to update the list of
|
|
|
|
// servers for a given agent.
|
2015-09-25 03:39:03 +00:00
|
|
|
func (s *HTTPServer) AgentServersRequest(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
|
|
|
switch req.Method {
|
|
|
|
case "PUT", "POST":
|
|
|
|
return s.updateServers(resp, req)
|
|
|
|
case "GET":
|
|
|
|
return s.listServers(resp, req)
|
|
|
|
default:
|
|
|
|
return nil, CodedError(405, ErrInvalidMethod)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *HTTPServer) listServers(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
|
|
|
client := s.agent.Client()
|
|
|
|
if client == nil {
|
|
|
|
return nil, CodedError(501, ErrInvalidMethod)
|
|
|
|
}
|
|
|
|
|
2017-10-06 19:02:53 +00:00
|
|
|
var secret string
|
|
|
|
s.parseToken(req, &secret)
|
|
|
|
|
|
|
|
// Check agent read permissions
|
|
|
|
if aclObj, err := s.agent.Client().ResolveToken(secret); err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if aclObj != nil && !aclObj.AllowAgentRead() {
|
|
|
|
return nil, structs.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
|
2016-09-22 00:06:52 +00:00
|
|
|
peers := s.agent.client.GetServers()
|
2017-09-14 17:04:48 +00:00
|
|
|
sort.Strings(peers)
|
2016-05-23 18:09:31 +00:00
|
|
|
return peers, nil
|
2015-09-25 03:39:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *HTTPServer) updateServers(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
|
|
|
client := s.agent.Client()
|
|
|
|
if client == nil {
|
|
|
|
return nil, CodedError(501, ErrInvalidMethod)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the servers from the request
|
|
|
|
servers := req.URL.Query()["address"]
|
|
|
|
if len(servers) == 0 {
|
|
|
|
return nil, CodedError(400, "missing server address")
|
|
|
|
}
|
|
|
|
|
2017-10-06 19:02:53 +00:00
|
|
|
var secret string
|
|
|
|
s.parseToken(req, &secret)
|
|
|
|
|
|
|
|
// Check agent write permissions
|
|
|
|
if aclObj, err := s.agent.Client().ResolveToken(secret); err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if aclObj != nil && !aclObj.AllowAgentWrite() {
|
|
|
|
return nil, structs.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
|
2015-09-25 03:39:03 +00:00
|
|
|
// Set the servers list into the client
|
2016-09-22 00:06:52 +00:00
|
|
|
s.agent.logger.Printf("[TRACE] Adding servers %+q to the client's primary server list", servers)
|
|
|
|
if err := client.SetServers(servers); err != nil {
|
|
|
|
s.agent.logger.Printf("[ERR] Attempt to add servers %q to client failed: %v", servers, err)
|
|
|
|
//TODO is this the right error to return?
|
|
|
|
return nil, CodedError(400, err.Error())
|
2016-05-23 18:09:31 +00:00
|
|
|
}
|
2015-09-25 03:39:03 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2016-10-17 17:48:04 +00:00
|
|
|
// KeyringOperationRequest allows an operator to install/delete/use keys
|
|
|
|
func (s *HTTPServer) KeyringOperationRequest(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
|
|
|
srv := s.agent.Server()
|
|
|
|
if srv == nil {
|
|
|
|
return nil, CodedError(501, ErrInvalidMethod)
|
|
|
|
}
|
|
|
|
|
2017-10-06 19:02:53 +00:00
|
|
|
var secret string
|
|
|
|
s.parseToken(req, &secret)
|
|
|
|
|
|
|
|
// Check agent write permissions
|
2017-10-09 23:09:44 +00:00
|
|
|
if aclObj, err := srv.ResolveToken(secret); err != nil {
|
2017-10-06 19:02:53 +00:00
|
|
|
return nil, err
|
|
|
|
} else if aclObj != nil && !aclObj.AllowAgentWrite() {
|
|
|
|
return nil, structs.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
|
2016-10-17 17:48:04 +00:00
|
|
|
kmgr := srv.KeyManager()
|
|
|
|
var sresp *serf.KeyResponse
|
|
|
|
var err error
|
|
|
|
|
|
|
|
// Get the key from the req body
|
|
|
|
var args structs.KeyringRequest
|
|
|
|
|
|
|
|
//Get the op
|
|
|
|
op := strings.TrimPrefix(req.URL.Path, "/v1/agent/keyring/")
|
|
|
|
|
|
|
|
switch op {
|
|
|
|
case "list":
|
|
|
|
sresp, err = kmgr.ListKeys()
|
|
|
|
case "install":
|
|
|
|
if err := decodeBody(req, &args); err != nil {
|
|
|
|
return nil, CodedError(500, err.Error())
|
|
|
|
}
|
|
|
|
sresp, err = kmgr.InstallKey(args.Key)
|
|
|
|
case "use":
|
|
|
|
if err := decodeBody(req, &args); err != nil {
|
|
|
|
return nil, CodedError(500, err.Error())
|
|
|
|
}
|
|
|
|
sresp, err = kmgr.UseKey(args.Key)
|
|
|
|
case "remove":
|
|
|
|
if err := decodeBody(req, &args); err != nil {
|
|
|
|
return nil, CodedError(500, err.Error())
|
|
|
|
}
|
|
|
|
sresp, err = kmgr.RemoveKey(args.Key)
|
|
|
|
default:
|
|
|
|
return nil, CodedError(404, "resource not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
kresp := structs.KeyringResponse{
|
|
|
|
Messages: sresp.Messages,
|
|
|
|
Keys: sresp.Keys,
|
|
|
|
NumNodes: sresp.NumNodes,
|
|
|
|
}
|
|
|
|
return kresp, nil
|
|
|
|
}
|
|
|
|
|
2015-09-07 02:08:05 +00:00
|
|
|
type agentSelf struct {
|
|
|
|
Config *Config `json:"config"`
|
2015-09-11 22:19:26 +00:00
|
|
|
Member Member `json:"member,omitempty"`
|
2015-09-07 02:08:05 +00:00
|
|
|
Stats map[string]map[string]string `json:"stats"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type joinResult struct {
|
|
|
|
NumJoined int `json:"num_joined"`
|
|
|
|
Error string `json:"error"`
|
|
|
|
}
|