consul/agent/session_endpoint.go

223 lines
6.8 KiB
Go
Raw Permalink Normal View History

// Copyright (c) HashiCorp, Inc.
[COMPLIANCE] License changes (#18443) * Adding explicit MPL license for sub-package This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Adding explicit MPL license for sub-package This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Updating the license from MPL to Business Source License Going forward, this project will be licensed under the Business Source License v1.1. Please see our blog post for more details at <Blog URL>, FAQ at www.hashicorp.com/licensing-faq, and details of the license at www.hashicorp.com/bsl. * add missing license headers * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 --------- Co-authored-by: hashicorp-copywrite[bot] <110428419+hashicorp-copywrite[bot]@users.noreply.github.com>
2023-08-11 13:12:13 +00:00
// SPDX-License-Identifier: BUSL-1.1
2014-05-16 22:49:17 +00:00
package agent
import (
"fmt"
"net/http"
2022-06-01 17:17:14 +00:00
"strings"
"time"
2014-12-13 03:17:04 +00:00
"github.com/hashicorp/consul/agent/structs"
"github.com/hashicorp/consul/lib"
"github.com/hashicorp/consul/types"
)
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 *HTTPHandlers) SessionCreate(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
// 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{
Node: s.agent.config.NodeName,
NodeChecks: []string{string(structs.SerfCheckID)},
Checks: []types.CheckID{structs.SerfCheckID},
LockDelay: 15 * time.Second,
Behavior: structs.SessionKeysRelease,
TTL: "",
2014-05-16 22:49:17 +00:00
},
}
s.parseDC(req, &args.Datacenter)
s.parseToken(req, &args.Token)
if err := s.parseEntMetaNoWildcard(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 {
if err := s.rewordUnknownEnterpriseFieldError(lib.DecodeJSON(req.Body, &args.Session)); err != nil {
return nil, HTTPError{StatusCode: http.StatusBadRequest, Reason: fmt.Sprintf("Request decode failed: %v", err)}
2014-05-16 22:49:17 +00:00
}
}
fixupEmptySessionChecks(&args.Session)
if (s.agent.config.Datacenter != args.Datacenter) && (!s.agent.config.ServerMode) {
return nil, fmt.Errorf("cross datacenter lock must be created at server agent")
}
2014-05-16 22:49:17 +00:00
// Create the session, get the ID
var out string
if err := s.agent.RPC(req.Context(), "Session.Apply", &args, &out); err != nil {
2014-05-16 22:49:17 +00:00
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
}
// SessionDestroy is used to destroy an existing session
func (s *HTTPHandlers) SessionDestroy(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
2014-05-16 22:49:17 +00:00
args := structs.SessionRequest{
Op: structs.SessionDestroy,
}
s.parseDC(req, &args.Datacenter)
s.parseToken(req, &args.Token)
if err := s.parseEntMetaNoWildcard(req, &args.Session.EnterpriseMeta); err != nil {
return nil, err
}
2014-05-16 22:49:17 +00:00
// Pull out the session id
2022-06-01 17:17:14 +00:00
args.Session.ID = strings.TrimPrefix(req.URL.Path, "/v1/session/destroy/")
2014-05-16 22:49:17 +00:00
if args.Session.ID == "" {
return nil, HTTPError{StatusCode: http.StatusBadRequest, Reason: "Missing session"}
2014-05-16 22:49:17 +00:00
}
var out string
if err := s.agent.RPC(req.Context(), "Session.Apply", &args, &out); err != nil {
2014-05-16 22:49:17 +00:00
return nil, err
}
return true, nil
}
// SessionRenew is used to renew the TTL on an existing TTL session
func (s *HTTPHandlers) 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
}
if err := s.parseEntMetaNoWildcard(req, &args.EnterpriseMeta); err != nil {
return nil, err
}
// Pull out the session id
2022-06-01 17:17:14 +00:00
args.SessionID = strings.TrimPrefix(req.URL.Path, "/v1/session/renew/")
args.Session = args.SessionID
if args.SessionID == "" {
return nil, HTTPError{StatusCode: http.StatusBadRequest, Reason: "Missing session"}
}
var out structs.IndexedSessions
if err := s.agent.RPC(req.Context(), "Session.Renew", &args, &out); err != nil {
return nil, err
} else if out.Sessions == nil {
return nil, HTTPError{StatusCode: http.StatusNotFound, Reason: fmt.Sprintf("Session id '%s' not found", args.SessionID)}
}
return out.Sessions, nil
}
2014-05-16 22:49:17 +00:00
// SessionGet is used to get info for a particular session
func (s *HTTPHandlers) SessionGet(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
2014-05-16 22:49:17 +00:00
args := structs.SessionSpecificRequest{}
if done := s.parse(resp, req, &args.Datacenter, &args.QueryOptions); done {
return nil, nil
}
if err := s.parseEntMetaNoWildcard(req, &args.EnterpriseMeta); err != nil {
return nil, err
}
2014-05-16 22:49:17 +00:00
// Pull out the session id
2022-06-01 17:17:14 +00:00
args.SessionID = strings.TrimPrefix(req.URL.Path, "/v1/session/info/")
args.Session = args.SessionID
if args.SessionID == "" {
return nil, HTTPError{StatusCode: http.StatusBadRequest, Reason: "Missing session"}
2014-05-16 22:49:17 +00:00
}
var out structs.IndexedSessions
defer setMeta(resp, &out.QueryMeta)
if err := s.agent.RPC(req.Context(), "Session.Get", &args, &out); err != nil {
2014-05-16 22:49:17 +00:00
return nil, err
}
// 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 *HTTPHandlers) SessionList(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
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
}
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(req.Context(), "Session.List", &args, &out); err != nil {
2014-05-16 22:49:17 +00:00
return nil, err
}
// 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 *HTTPHandlers) SessionsForNode(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
2014-05-16 22:49:17 +00:00
args := structs.NodeSpecificRequest{}
if done := s.parse(resp, req, &args.Datacenter, &args.QueryOptions); done {
return nil, nil
}
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
2022-06-01 17:17:14 +00:00
args.Node = strings.TrimPrefix(req.URL.Path, "/v1/session/node/")
2014-05-16 22:49:17 +00:00
if args.Node == "" {
return nil, HTTPError{StatusCode: http.StatusBadRequest, Reason: "Missing node name"}
2014-05-16 22:49:17 +00:00
}
var out structs.IndexedSessions
defer setMeta(resp, &out.QueryMeta)
if err := s.agent.RPC(req.Context(), "Session.NodeSessions", &args, &out); err != nil {
2014-05-16 22:49:17 +00:00
return nil, err
}
// 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
}
// This is for backwards compatibility. Prior to 1.7.0 users could create a session with no Checks
// by passing an empty Checks field. Now the preferred field is session.NodeChecks.
func fixupEmptySessionChecks(session *structs.Session) {
// If the Checks field contains an empty slice, empty out the default check that was provided to NodeChecks
if len(session.Checks) == 0 {
session.NodeChecks = make([]string, 0)
return
}
// If the checks field contains the default value, empty it out. Defer to what is in NodeChecks.
if len(session.Checks) == 1 && session.Checks[0] == structs.SerfCheckID {
session.Checks = nil
return
}
// If the NodeChecks field contains an empty slice, empty out the default check that was provided to Checks
if len(session.NodeChecks) == 0 {
session.Checks = nil
return
}
return
}