summaryrefslogtreecommitdiff
path: root/daemon/kmd/api/api.go
blob: 084b6f882b0bd5c10dd060debf55e60d7c111bfe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// Copyright (C) 2019-2024 Algorand, Inc.
// This file is part of go-algorand
//
// go-algorand is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// go-algorand is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with go-algorand.  If not, see <https://www.gnu.org/licenses/>.

// Package api for KMD HTTP API
//
// API for KMD (Key Management Daemon)
//
//	Schemes: http
//	Host: localhost
//	BasePath: /
//	Version: 0.0.1
//	License:
//	Contact: contact@algorand.com
//
//	Consumes:
//	- application/json
//
//	Produces:
//	- application/json
//
//	Security:
//	- api_key:
//
//	SecurityDefinitions:
//	api_key:
//	  type: apiKey
//	  name: X-KMD-API-Token
//	  in: header
//	  description: >-
//	    Generated header parameter. This value can be found in `/kmd/data/dir/kmd.token`. Example value:
//	    '330b2e4fc9b20f4f89812cf87f1dabeb716d23e3f11aec97a61ff5f750563b78'
//	  required: true
//	  x-example: 330b2e4fc9b20f4f89812cf87f1dabeb716d23e3f11aec97a61ff5f750563b78
//
// swagger:meta
// ---
// IF YOU MODIFY SUBPACKAGES: IMPORTANT
// MAKE SURE YOU REGENERATE THE SWAGGER SPEC (using go:generate)
// MAKE SURE IT VALIDATES
//
// Currently, server implementation annotations serve
// as the API ground truth. From that, we use go-swagger
// to generate a swagger spec.
//
// Autogenerate the swagger json.
// Base path must be a fully specified package name (else, it seems that swagger feeds a relative path to
// loader.Config.Import(), and that breaks the vendor directory if the source is symlinked from elsewhere)
//
//go:generate swagger generate spec -m -o="./swagger.json"
//go:generate swagger validate ./swagger.json --stop-on-error
//go:generate sh ../lib/kmdapi/bundle_swagger_json.sh
package api

import (
	"fmt"
	"net/http"

	"github.com/gorilla/mux"

	"github.com/algorand/go-algorand/daemon/kmd/api/v1"
	"github.com/algorand/go-algorand/daemon/kmd/lib/kmdapi"
	"github.com/algorand/go-algorand/daemon/kmd/session"
	"github.com/algorand/go-algorand/logging"
	"github.com/algorand/go-algorand/protocol"
)

const (
	apiV1Tag = "v1"
)

var supportedAPIVersions = []string{apiV1Tag}

// The /versions endpoint is one of two non-versioned API endpoints, since its
// response tells us which API versions are supported (the other is /swagger.json)
func versionsHandler(w http.ResponseWriter, r *http.Request) {
	// swagger:operation GET /versions GetVersion
	//---
	//     Summary: Retrieves the current version
	//     Produces:
	//     - application/json
	//     Parameters:
	//     - name: Versions Request
	//       in: body
	//       required: false
	//       schema:
	//         "$ref": "#/definitions/VersionsRequest"
	//     Responses:
	//       "200":
	//         "$ref": "#/responses/VersionsResponse"
	w.Header().Set("Content-Type", "application/json")
	w.WriteHeader(http.StatusOK)
	response := kmdapi.VersionsResponse{
		Versions: supportedAPIVersions,
	}
	w.Write(protocol.EncodeJSON(response))
}

// optionsHandler is a dummy endpoint that catches all OPTIONS requests. We
// need this because middleware only triggers if we match a route.
func optionsHandler(w http.ResponseWriter, r *http.Request) {
	w.WriteHeader(http.StatusOK)
}

// SwaggerHandler is an httpHandler for route GET /swagger.json, and at this point
// is not versioned.
func SwaggerHandler(w http.ResponseWriter, r *http.Request) {
	// swagger:operation GET /swagger.json SwaggerHandler
	//---
	//     Summary: Gets the current swagger spec.
	//     Description: Returns the entire swagger spec in json.
	//     Produces:
	//     - application/json
	//     Schemes:
	//     - http
	//     Responses:
	//       200:
	//         description: The current swagger spec
	//         schema: {type: string}
	//       default: { description: Unknown Error }
	w.Header().Set("Content-Type", "application/json")
	w.WriteHeader(http.StatusOK)
	w.Write([]byte(kmdapi.SwaggerSpecJSON))
}

// Handler returns the root mux router for the kmd API. It sets up handlers on
// subrouters specific to each API version.
func Handler(sm *session.Manager, log logging.Logger, allowedOrigins []string, apiToken string, reqCB func()) *mux.Router {
	rootRouter := mux.NewRouter()

	// Send the appropriate CORS headers
	rootRouter.Use(corsMiddleware(allowedOrigins))

	// Handle OPTIONS requests
	rootRouter.Methods("OPTIONS").HandlerFunc(optionsHandler)

	// The /versions endpoint has no version, so we register it here. /versions
	// has no auth, because it doesn't return anything sensitive, and auth is
	// version-specific. The same applies for /swagger.json.
	rootRouter.HandleFunc("/versions", versionsHandler)
	rootRouter.HandleFunc("/swagger.json", SwaggerHandler)

	// Handle API V1 routes at /v1/<...>
	v1Router := rootRouter.PathPrefix(fmt.Sprintf("/%s", apiV1Tag)).Subrouter()
	v1.RegisterHandlers(v1Router, sm, log, apiToken, reqCB)

	return rootRouter
}