2014-05-16 22:49:17 +00:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
2014-05-19 20:12:15 +00:00
|
|
|
"time"
|
2014-12-13 03:17:04 +00:00
|
|
|
|
2017-07-06 10:34:00 +00:00
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
2019-12-06 16:14:56 +00:00
|
|
|
"github.com/hashicorp/consul/lib"
|
2016-06-07 20:24:51 +00:00
|
|
|
"github.com/hashicorp/consul/types"
|
2014-05-19 20:12:15 +00:00
|
|
|
)
|
|
|
|
|
2014-05-19 18:29:50 +00:00
|
|
|
// sessionCreateResponse is used to wrap the session ID
|
|
|
|
type sessionCreateResponse struct {
|
|
|
|
ID string
|
|
|
|
}
|
|
|
|
|
2014-05-16 22:49:17 +00:00
|
|
|
// SessionCreate is used to create a new session
|
|
|
|
func (s *HTTPServer) SessionCreate(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
2017-12-14 01:25:29 +00:00
|
|
|
// Default the session to our node + serf check + release session
|
|
|
|
// invalidate behavior.
|
2014-05-16 22:49:17 +00:00
|
|
|
args := structs.SessionRequest{
|
|
|
|
Op: structs.SessionCreate,
|
|
|
|
Session: structs.Session{
|
2014-05-19 20:12:15 +00:00
|
|
|
Node: s.agent.config.NodeName,
|
2017-07-14 05:33:47 +00:00
|
|
|
Checks: []types.CheckID{structs.SerfCheckID},
|
2014-05-19 20:12:15 +00:00
|
|
|
LockDelay: 15 * time.Second,
|
2014-11-20 02:11:41 +00:00
|
|
|
Behavior: structs.SessionKeysRelease,
|
2014-11-25 16:06:14 +00:00
|
|
|
TTL: "",
|
2014-05-16 22:49:17 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
s.parseDC(req, &args.Datacenter)
|
2016-12-13 05:59:22 +00:00
|
|
|
s.parseToken(req, &args.Token)
|
2019-12-06 16:14:56 +00:00
|
|
|
if err := s.parseEntMeta(req, &args.Session.EnterpriseMeta); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-05-16 22:49:17 +00:00
|
|
|
|
|
|
|
// Handle optional request body
|
|
|
|
if req.ContentLength > 0 {
|
2019-12-06 16:14:56 +00:00
|
|
|
if err := s.rewordUnknownEnterpriseFieldError(lib.DecodeJSON(req.Body, &args.Session)); err != nil {
|
2017-08-23 19:19:11 +00:00
|
|
|
resp.WriteHeader(http.StatusBadRequest)
|
Use fmt.Fprint/Fprintf/Fprintln
Used the following rewrite rules:
gofmt -w -r 'resp.Write([]byte(fmt.Sprintf(a, b, c, d))) -> fmt.Fprintf(resp, a, b, c, d)' *.go
gofmt -w -r 'resp.Write([]byte(fmt.Sprintf(a, b, c))) -> fmt.Fprintf(resp, a, b, c)' *.go
gofmt -w -r 'resp.Write([]byte(fmt.Sprintf(a, b))) -> fmt.Fprintf(resp, a, b)' *.go
gofmt -w -r 'resp.Write([]byte(fmt.Sprintf(a))) -> fmt.Fprint(resp, a)' *.go
gofmt -w -r 'resp.Write([]byte(a + "\n")) -> fmt.Fprintln(resp, a)' *.go
gofmt -w -r 'resp.Write([]byte(a)) -> fmt.Fprint(resp, a)' *.go
2017-04-20 14:07:42 +00:00
|
|
|
fmt.Fprintf(resp, "Request decode failed: %v", err)
|
2014-05-16 22:49:17 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the session, get the ID
|
|
|
|
var out string
|
|
|
|
if err := s.agent.RPC("Session.Apply", &args, &out); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Format the response as a JSON object
|
2014-05-19 18:29:50 +00:00
|
|
|
return sessionCreateResponse{out}, nil
|
2014-05-16 22:49:17 +00:00
|
|
|
}
|
|
|
|
|
2017-12-14 01:25:29 +00:00
|
|
|
// FixupChecks is used to handle parsing the JSON body to default-add the Serf
|
|
|
|
// health check if they didn't specify any checks, but to allow an empty list
|
|
|
|
// to take out the Serf health check. This behavior broke when mapstructure was
|
|
|
|
// updated after 0.9.3, likely because we have a type wrapper around the string.
|
|
|
|
func FixupChecks(raw interface{}, s *structs.Session) error {
|
|
|
|
rawMap, ok := raw.(map[string]interface{})
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
for k := range rawMap {
|
|
|
|
if strings.ToLower(k) == "checks" {
|
|
|
|
// If they supplied a checks key in the JSON, then
|
|
|
|
// remove the default entries and respect whatever they
|
|
|
|
// specified.
|
|
|
|
s.Checks = nil
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-05-16 22:49:17 +00:00
|
|
|
// SessionDestroy is used to destroy an existing session
|
|
|
|
func (s *HTTPServer) SessionDestroy(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
|
|
|
args := structs.SessionRequest{
|
|
|
|
Op: structs.SessionDestroy,
|
|
|
|
}
|
|
|
|
s.parseDC(req, &args.Datacenter)
|
2016-12-13 05:59:22 +00:00
|
|
|
s.parseToken(req, &args.Token)
|
2019-12-06 16:14:56 +00:00
|
|
|
if err := s.parseEntMeta(req, &args.Session.EnterpriseMeta); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-05-16 22:49:17 +00:00
|
|
|
|
|
|
|
// Pull out the session id
|
|
|
|
args.Session.ID = strings.TrimPrefix(req.URL.Path, "/v1/session/destroy/")
|
|
|
|
if args.Session.ID == "" {
|
2017-08-23 19:19:11 +00:00
|
|
|
resp.WriteHeader(http.StatusBadRequest)
|
Use fmt.Fprint/Fprintf/Fprintln
Used the following rewrite rules:
gofmt -w -r 'resp.Write([]byte(fmt.Sprintf(a, b, c, d))) -> fmt.Fprintf(resp, a, b, c, d)' *.go
gofmt -w -r 'resp.Write([]byte(fmt.Sprintf(a, b, c))) -> fmt.Fprintf(resp, a, b, c)' *.go
gofmt -w -r 'resp.Write([]byte(fmt.Sprintf(a, b))) -> fmt.Fprintf(resp, a, b)' *.go
gofmt -w -r 'resp.Write([]byte(fmt.Sprintf(a))) -> fmt.Fprint(resp, a)' *.go
gofmt -w -r 'resp.Write([]byte(a + "\n")) -> fmt.Fprintln(resp, a)' *.go
gofmt -w -r 'resp.Write([]byte(a)) -> fmt.Fprint(resp, a)' *.go
2017-04-20 14:07:42 +00:00
|
|
|
fmt.Fprint(resp, "Missing session")
|
2014-05-16 22:49:17 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var out string
|
|
|
|
if err := s.agent.RPC("Session.Apply", &args, &out); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2014-11-25 16:06:14 +00:00
|
|
|
// SessionRenew is used to renew the TTL on an existing TTL session
|
|
|
|
func (s *HTTPServer) SessionRenew(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
|
|
|
args := structs.SessionSpecificRequest{}
|
|
|
|
if done := s.parse(resp, req, &args.Datacenter, &args.QueryOptions); done {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2019-12-06 16:14:56 +00:00
|
|
|
if err := s.parseEntMeta(req, &args.EnterpriseMeta); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-11-25 16:06:14 +00:00
|
|
|
|
|
|
|
// Pull out the session id
|
2019-11-25 17:07:04 +00:00
|
|
|
args.SessionID = strings.TrimPrefix(req.URL.Path, "/v1/session/renew/")
|
|
|
|
if args.SessionID == "" {
|
2017-08-23 19:19:11 +00:00
|
|
|
resp.WriteHeader(http.StatusBadRequest)
|
Use fmt.Fprint/Fprintf/Fprintln
Used the following rewrite rules:
gofmt -w -r 'resp.Write([]byte(fmt.Sprintf(a, b, c, d))) -> fmt.Fprintf(resp, a, b, c, d)' *.go
gofmt -w -r 'resp.Write([]byte(fmt.Sprintf(a, b, c))) -> fmt.Fprintf(resp, a, b, c)' *.go
gofmt -w -r 'resp.Write([]byte(fmt.Sprintf(a, b))) -> fmt.Fprintf(resp, a, b)' *.go
gofmt -w -r 'resp.Write([]byte(fmt.Sprintf(a))) -> fmt.Fprint(resp, a)' *.go
gofmt -w -r 'resp.Write([]byte(a + "\n")) -> fmt.Fprintln(resp, a)' *.go
gofmt -w -r 'resp.Write([]byte(a)) -> fmt.Fprint(resp, a)' *.go
2017-04-20 14:07:42 +00:00
|
|
|
fmt.Fprint(resp, "Missing session")
|
2014-11-25 16:06:14 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var out structs.IndexedSessions
|
|
|
|
if err := s.agent.RPC("Session.Renew", &args, &out); err != nil {
|
|
|
|
return nil, err
|
2014-12-10 15:02:23 +00:00
|
|
|
} else if out.Sessions == nil {
|
2017-08-23 19:19:11 +00:00
|
|
|
resp.WriteHeader(http.StatusNotFound)
|
2019-11-25 17:07:04 +00:00
|
|
|
fmt.Fprintf(resp, "Session id '%s' not found", args.SessionID)
|
2014-12-10 15:02:23 +00:00
|
|
|
return nil, nil
|
2014-11-25 16:06:14 +00:00
|
|
|
}
|
2014-12-10 15:02:23 +00:00
|
|
|
|
2014-11-25 16:06:14 +00:00
|
|
|
return out.Sessions, nil
|
|
|
|
}
|
|
|
|
|
2014-05-16 22:49:17 +00:00
|
|
|
// SessionGet is used to get info for a particular session
|
|
|
|
func (s *HTTPServer) SessionGet(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
|
|
|
args := structs.SessionSpecificRequest{}
|
|
|
|
if done := s.parse(resp, req, &args.Datacenter, &args.QueryOptions); done {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2019-12-06 16:14:56 +00:00
|
|
|
if err := s.parseEntMeta(req, &args.EnterpriseMeta); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-05-16 22:49:17 +00:00
|
|
|
|
|
|
|
// Pull out the session id
|
2019-11-25 17:07:04 +00:00
|
|
|
args.SessionID = strings.TrimPrefix(req.URL.Path, "/v1/session/info/")
|
|
|
|
if args.SessionID == "" {
|
2017-08-23 19:19:11 +00:00
|
|
|
resp.WriteHeader(http.StatusBadRequest)
|
Use fmt.Fprint/Fprintf/Fprintln
Used the following rewrite rules:
gofmt -w -r 'resp.Write([]byte(fmt.Sprintf(a, b, c, d))) -> fmt.Fprintf(resp, a, b, c, d)' *.go
gofmt -w -r 'resp.Write([]byte(fmt.Sprintf(a, b, c))) -> fmt.Fprintf(resp, a, b, c)' *.go
gofmt -w -r 'resp.Write([]byte(fmt.Sprintf(a, b))) -> fmt.Fprintf(resp, a, b)' *.go
gofmt -w -r 'resp.Write([]byte(fmt.Sprintf(a))) -> fmt.Fprint(resp, a)' *.go
gofmt -w -r 'resp.Write([]byte(a + "\n")) -> fmt.Fprintln(resp, a)' *.go
gofmt -w -r 'resp.Write([]byte(a)) -> fmt.Fprint(resp, a)' *.go
2017-04-20 14:07:42 +00:00
|
|
|
fmt.Fprint(resp, "Missing session")
|
2014-05-16 22:49:17 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var out structs.IndexedSessions
|
|
|
|
defer setMeta(resp, &out.QueryMeta)
|
|
|
|
if err := s.agent.RPC("Session.Get", &args, &out); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-11-15 05:05:37 +00:00
|
|
|
|
|
|
|
// Use empty list instead of nil
|
|
|
|
if out.Sessions == nil {
|
|
|
|
out.Sessions = make(structs.Sessions, 0)
|
|
|
|
}
|
2014-05-16 22:49:17 +00:00
|
|
|
return out.Sessions, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SessionList is used to list all the sessions
|
|
|
|
func (s *HTTPServer) SessionList(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
2019-11-25 17:07:04 +00:00
|
|
|
args := structs.SessionSpecificRequest{}
|
2014-05-16 22:49:17 +00:00
|
|
|
if done := s.parse(resp, req, &args.Datacenter, &args.QueryOptions); done {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2019-12-06 16:14:56 +00:00
|
|
|
if err := s.parseEntMeta(req, &args.EnterpriseMeta); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-05-16 22:49:17 +00:00
|
|
|
|
|
|
|
var out structs.IndexedSessions
|
|
|
|
defer setMeta(resp, &out.QueryMeta)
|
|
|
|
if err := s.agent.RPC("Session.List", &args, &out); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-11-15 05:05:37 +00:00
|
|
|
|
|
|
|
// Use empty list instead of nil
|
|
|
|
if out.Sessions == nil {
|
|
|
|
out.Sessions = make(structs.Sessions, 0)
|
|
|
|
}
|
2014-05-16 22:49:17 +00:00
|
|
|
return out.Sessions, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SessionsForNode returns all the nodes belonging to a node
|
|
|
|
func (s *HTTPServer) SessionsForNode(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
|
|
|
args := structs.NodeSpecificRequest{}
|
|
|
|
if done := s.parse(resp, req, &args.Datacenter, &args.QueryOptions); done {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2019-12-06 16:14:56 +00:00
|
|
|
if err := s.parseEntMeta(req, &args.EnterpriseMeta); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-05-16 22:49:17 +00:00
|
|
|
|
|
|
|
// Pull out the node name
|
|
|
|
args.Node = strings.TrimPrefix(req.URL.Path, "/v1/session/node/")
|
|
|
|
if args.Node == "" {
|
2017-08-23 19:19:11 +00:00
|
|
|
resp.WriteHeader(http.StatusBadRequest)
|
Use fmt.Fprint/Fprintf/Fprintln
Used the following rewrite rules:
gofmt -w -r 'resp.Write([]byte(fmt.Sprintf(a, b, c, d))) -> fmt.Fprintf(resp, a, b, c, d)' *.go
gofmt -w -r 'resp.Write([]byte(fmt.Sprintf(a, b, c))) -> fmt.Fprintf(resp, a, b, c)' *.go
gofmt -w -r 'resp.Write([]byte(fmt.Sprintf(a, b))) -> fmt.Fprintf(resp, a, b)' *.go
gofmt -w -r 'resp.Write([]byte(fmt.Sprintf(a))) -> fmt.Fprint(resp, a)' *.go
gofmt -w -r 'resp.Write([]byte(a + "\n")) -> fmt.Fprintln(resp, a)' *.go
gofmt -w -r 'resp.Write([]byte(a)) -> fmt.Fprint(resp, a)' *.go
2017-04-20 14:07:42 +00:00
|
|
|
fmt.Fprint(resp, "Missing node name")
|
2014-05-16 22:49:17 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var out structs.IndexedSessions
|
|
|
|
defer setMeta(resp, &out.QueryMeta)
|
|
|
|
if err := s.agent.RPC("Session.NodeSessions", &args, &out); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-11-15 05:05:37 +00:00
|
|
|
|
|
|
|
// Use empty list instead of nil
|
|
|
|
if out.Sessions == nil {
|
|
|
|
out.Sessions = make(structs.Sessions, 0)
|
|
|
|
}
|
2014-05-16 22:49:17 +00:00
|
|
|
return out.Sessions, nil
|
|
|
|
}
|