From 0103622cadd7c1094a3277740515549cd49c07d1 Mon Sep 17 00:00:00 2001 From: Buck Doyle Date: Wed, 3 Feb 2021 14:54:28 -0600 Subject: [PATCH 1/4] Change to return 204 for OSS /v1/operator/license MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This is quite naïve but it’s working for me locally! --- command/agent/http.go | 1 + command/agent/http_oss.go | 2 -- command/agent/operator_endpoint.go | 11 +++++++++++ 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/command/agent/http.go b/command/agent/http.go index bbf071cbe0e0..c7568fd6e2e1 100644 --- a/command/agent/http.go +++ b/command/agent/http.go @@ -314,6 +314,7 @@ func (s *HTTPServer) registerHandlers(enableDebug bool) { s.mux.HandleFunc("/v1/search", s.wrap(s.SearchRequest)) + s.mux.HandleFunc("/v1/operator/license", s.wrap(s.LicenseRequest)) s.mux.HandleFunc("/v1/operator/raft/", s.wrap(s.OperatorRequest)) s.mux.HandleFunc("/v1/operator/autopilot/configuration", s.wrap(s.OperatorAutopilotConfiguration)) s.mux.HandleFunc("/v1/operator/autopilot/health", s.wrap(s.OperatorServerHealth)) diff --git a/command/agent/http_oss.go b/command/agent/http_oss.go index e90818141501..863ab4bc27ca 100644 --- a/command/agent/http_oss.go +++ b/command/agent/http_oss.go @@ -16,8 +16,6 @@ func (s *HTTPServer) registerEnterpriseHandlers() { s.mux.HandleFunc("/v1/quota/", s.wrap(s.entOnly)) s.mux.HandleFunc("/v1/quota", s.wrap(s.entOnly)) - s.mux.HandleFunc("/v1/operator/license", s.wrap(s.entOnly)) - s.mux.HandleFunc("/v1/recommendation", s.wrap(s.entOnly)) s.mux.HandleFunc("/v1/recommendations", s.wrap(s.entOnly)) s.mux.HandleFunc("/v1/recommendations/apply", s.wrap(s.entOnly)) diff --git a/command/agent/operator_endpoint.go b/command/agent/operator_endpoint.go index ed4a3c4cb732..f2bd6b205614 100644 --- a/command/agent/operator_endpoint.go +++ b/command/agent/operator_endpoint.go @@ -289,6 +289,17 @@ func (s *HTTPServer) schedulerUpdateConfig(resp http.ResponseWriter, req *http.R return reply, nil } +func (s *HTTPServer) LicenseRequest(resp http.ResponseWriter, req *http.Request) (interface{}, error) { + switch req.Method { + case "GET": + resp.WriteHeader(http.StatusNoContent) + return "", nil + default: + return nil, CodedError(405, ErrInvalidMethod) + } + +} + func (s *HTTPServer) SnapshotRequest(resp http.ResponseWriter, req *http.Request) (interface{}, error) { switch req.Method { case "GET": From d3a64ffb64452e56b7947866ec717a2670a4dffc Mon Sep 17 00:00:00 2001 From: Buck Doyle Date: Thu, 4 Feb 2021 16:29:47 -0600 Subject: [PATCH 2/4] Change license get call to handle expected 204 --- api/operator.go | 22 +++++++++++++++++-- command/agent/operator_endpoint.go | 2 +- .../hashicorp/nomad/api/operator.go | 22 +++++++++++++++++-- 3 files changed, 41 insertions(+), 5 deletions(-) diff --git a/api/operator.go b/api/operator.go index d5bc5d061d56..a4d8c56c2d11 100644 --- a/api/operator.go +++ b/api/operator.go @@ -1,6 +1,8 @@ package api import ( + "encoding/json" + "errors" "io" "io/ioutil" "strconv" @@ -297,10 +299,26 @@ func (op *Operator) LicensePut(license string, q *WriteOptions) (*WriteMeta, err } func (op *Operator) LicenseGet(q *QueryOptions) (*LicenseReply, *QueryMeta, error) { + req, err := op.c.newRequest("GET", "/v1/operator/license") + if err != nil { + return nil, nil, err + } + var reply LicenseReply - qm, err := op.c.query("/v1/operator/license", &reply, q) + _, resp, err := op.c.doRequest(req) if err != nil { return nil, nil, err } - return &reply, qm, nil + defer resp.Body.Close() + + if resp.StatusCode == 204 { + return nil, nil, errors.New("Nomad Enterprise only endpoint") + } + + err = json.NewDecoder(resp.Body).Decode(&reply) + if err == nil { + return &reply, nil, nil + } + + return nil, nil, err } diff --git a/command/agent/operator_endpoint.go b/command/agent/operator_endpoint.go index f2bd6b205614..c1084e1574d2 100644 --- a/command/agent/operator_endpoint.go +++ b/command/agent/operator_endpoint.go @@ -293,7 +293,7 @@ func (s *HTTPServer) LicenseRequest(resp http.ResponseWriter, req *http.Request) switch req.Method { case "GET": resp.WriteHeader(http.StatusNoContent) - return "", nil + return nil, nil default: return nil, CodedError(405, ErrInvalidMethod) } diff --git a/vendor/github.com/hashicorp/nomad/api/operator.go b/vendor/github.com/hashicorp/nomad/api/operator.go index d5bc5d061d56..a4d8c56c2d11 100644 --- a/vendor/github.com/hashicorp/nomad/api/operator.go +++ b/vendor/github.com/hashicorp/nomad/api/operator.go @@ -1,6 +1,8 @@ package api import ( + "encoding/json" + "errors" "io" "io/ioutil" "strconv" @@ -297,10 +299,26 @@ func (op *Operator) LicensePut(license string, q *WriteOptions) (*WriteMeta, err } func (op *Operator) LicenseGet(q *QueryOptions) (*LicenseReply, *QueryMeta, error) { + req, err := op.c.newRequest("GET", "/v1/operator/license") + if err != nil { + return nil, nil, err + } + var reply LicenseReply - qm, err := op.c.query("/v1/operator/license", &reply, q) + _, resp, err := op.c.doRequest(req) if err != nil { return nil, nil, err } - return &reply, qm, nil + defer resp.Body.Close() + + if resp.StatusCode == 204 { + return nil, nil, errors.New("Nomad Enterprise only endpoint") + } + + err = json.NewDecoder(resp.Body).Decode(&reply) + if err == nil { + return &reply, nil, nil + } + + return nil, nil, err } From f5b33da02db8dbafb435fbe5ddb3e28f76de186c Mon Sep 17 00:00:00 2001 From: Buck Doyle Date: Fri, 5 Feb 2021 14:48:12 -0600 Subject: [PATCH 3/4] Add 501 error for license PUT --- command/agent/operator_endpoint.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/command/agent/operator_endpoint.go b/command/agent/operator_endpoint.go index c1084e1574d2..71ee9d906de8 100644 --- a/command/agent/operator_endpoint.go +++ b/command/agent/operator_endpoint.go @@ -294,6 +294,8 @@ func (s *HTTPServer) LicenseRequest(resp http.ResponseWriter, req *http.Request) case "GET": resp.WriteHeader(http.StatusNoContent) return nil, nil + case "PUT": + return nil, CodedError(501, ErrEntOnly) default: return nil, CodedError(405, ErrInvalidMethod) } From f931960deb95618ed8beae6f9a1609393fe69108 Mon Sep 17 00:00:00 2001 From: Buck Doyle Date: Mon, 8 Feb 2021 10:18:10 -0600 Subject: [PATCH 4/4] Move OSS license-handling to non-ENT file --- command/agent/operator_endpoint.go | 13 ------------- command/agent/operator_endpoint_oss.go | 20 ++++++++++++++++++++ 2 files changed, 20 insertions(+), 13 deletions(-) create mode 100644 command/agent/operator_endpoint_oss.go diff --git a/command/agent/operator_endpoint.go b/command/agent/operator_endpoint.go index 71ee9d906de8..ed4a3c4cb732 100644 --- a/command/agent/operator_endpoint.go +++ b/command/agent/operator_endpoint.go @@ -289,19 +289,6 @@ func (s *HTTPServer) schedulerUpdateConfig(resp http.ResponseWriter, req *http.R return reply, nil } -func (s *HTTPServer) LicenseRequest(resp http.ResponseWriter, req *http.Request) (interface{}, error) { - switch req.Method { - case "GET": - resp.WriteHeader(http.StatusNoContent) - return nil, nil - case "PUT": - return nil, CodedError(501, ErrEntOnly) - default: - return nil, CodedError(405, ErrInvalidMethod) - } - -} - func (s *HTTPServer) SnapshotRequest(resp http.ResponseWriter, req *http.Request) (interface{}, error) { switch req.Method { case "GET": diff --git a/command/agent/operator_endpoint_oss.go b/command/agent/operator_endpoint_oss.go new file mode 100644 index 000000000000..d589afebebcd --- /dev/null +++ b/command/agent/operator_endpoint_oss.go @@ -0,0 +1,20 @@ +// +build !ent + +package agent + +import ( + "net/http" +) + +func (s *HTTPServer) LicenseRequest(resp http.ResponseWriter, req *http.Request) (interface{}, error) { + switch req.Method { + case "GET": + resp.WriteHeader(http.StatusNoContent) + return nil, nil + case "PUT": + return nil, CodedError(501, ErrEntOnly) + default: + return nil, CodedError(405, ErrInvalidMethod) + } + +}