| 1 | // Copyright 2020 Matthew Holt and The Caddy Authors |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package caddypkiscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddypki`/ |
| 16 | |
| 17 | import ( |
| 18 | "encoding/jsonscip-go gomod github.com/golang/go/src go1.25.1 `encoding/json`/" |
| 19 | "fmtscip-go gomod github.com/golang/go/src go1.25.1 fmt/" |
| 20 | "net/httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/" |
| 21 | "stringsscip-go gomod github.com/golang/go/src go1.25.1 strings/" |
| 22 | |
| 23 | "go.uber.org/zapscip-go gomod go.uber.org/zap v1.28.0 `go.uber.org/zap`/" |
| 24 | |
| 25 | "github.com/caddyserver/caddy/v2scip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/" |
| 26 | ) |
| 27 | |
| 28 | func initscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddypki`/init().() { |
| 29 | caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.RegisterModulescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/RegisterModule().(adminAPIscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddypki`/adminAPI#{}) |
| 30 | } |
| 31 | |
| 32 | // adminAPI is a module that serves PKI endpoints to retrieve |
| 33 | // information about the CAs being managed by Caddy. |
| 34 | type adminAPIscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddypki`/adminAPI# struct { |
| 35 | ctxscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddypki`/adminAPI#ctx. caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.Contextscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/Context# |
| 36 | logscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddypki`/adminAPI#log. *zapscip-go gomod go.uber.org/zap v1.28.0 `go.uber.org/zap`/.Loggerscip-go gomod go.uber.org/zap v1.28.0 `go.uber.org/zap`/Logger# |
| 37 | pkiAppscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddypki`/adminAPI#pkiApp. *PKIscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddypki`/PKI# |
| 38 | } |
| 39 | |
| 40 | // CaddyModule returns the Caddy module information. |
| 41 | func (adminAPIscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddypki`/adminAPI#) CaddyModulescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddypki`/adminAPI#CaddyModule().() caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.ModuleInfoscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/ModuleInfo# { |
| 42 | return caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.ModuleInfoscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/ModuleInfo#{ |
| 43 | IDscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/ModuleInfo#ID.: "admin.api.pki", |
| 44 | Newscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/ModuleInfo#New.: func() caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.Modulescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/Module# { return new(adminAPIscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddypki`/adminAPI#) }, |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | // Provision sets up the adminAPI module. |
| 49 | func (a *adminAPIscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddypki`/adminAPI#) Provisionscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddypki`/adminAPI#Provision().(ctx caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.Contextscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/Context#) error { |
| 50 | a.ctxscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddypki`/adminAPI#ctx. = ctx |
| 51 | a.logscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddypki`/adminAPI#log. = ctx.Loggerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/Context#Logger().(a) // TODO: passing in 'a' is a hack until the admin API is officially extensible (see #5032) |
| 52 | |
| 53 | // Avoid initializing PKI if it wasn't configured. |
| 54 | // We intentionally ignore the error since it's not |
| 55 | // fatal if the PKI app is not explicitly configured. |
| 56 | pkiApp, err := ctx.AppIfConfiguredscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/Context#AppIfConfigured().("pki") |
| 57 | if err == nil { |
| 58 | a.pkiAppscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddypki`/adminAPI#pkiApp. = pkiApp.(*PKIscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddypki`/PKI#) |
| 59 | } |
| 60 | |
| 61 | return nil |
| 62 | } |
| 63 | |
| 64 | // Routes returns the admin routes for the PKI app. |
| 65 | func (a *adminAPIscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddypki`/adminAPI#) Routesscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddypki`/adminAPI#Routes().() []caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.AdminRoutescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/AdminRoute# { |
| 66 | return []caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.AdminRoutescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/AdminRoute#{ |
| 67 | { |
| 68 | Patternscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/AdminRoute#Pattern.: adminPKIEndpointBasescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddypki`/adminPKIEndpointBase., |
| 69 | Handlerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/AdminRoute#Handler.: caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.AdminHandlerFuncscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/AdminHandlerFunc#(a.handleAPIEndpointsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddypki`/adminAPI#handleAPIEndpoints().), |
| 70 | }, |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | // handleAPIEndpoints routes API requests within adminPKIEndpointBase. |
| 75 | func (a *adminAPIscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddypki`/adminAPI#) handleAPIEndpointsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddypki`/adminAPI#handleAPIEndpoints().(w httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.ResponseWriterscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/ResponseWriter#, r *httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.Requestscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#) error { |
| 76 | uri := stringsscip-go gomod github.com/golang/go/src go1.25.1 strings/.TrimPrefixscip-go gomod github.com/golang/go/src go1.25.1 strings/TrimPrefix().(r.URLscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#URL..Pathscip-go gomod github.com/golang/go/src go1.25.1 `net/url`/URL#Path., "/pki/") |
| 77 | parts := stringsscip-go gomod github.com/golang/go/src go1.25.1 strings/.Splitscip-go gomod github.com/golang/go/src go1.25.1 strings/Split().(uri, "/") |
| 78 | switch { |
| 79 | case len(parts) == 2 && parts[0] == "ca" && parts[1] != "": |
| 80 | return a.handleCAInfoscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddypki`/adminAPI#handleCAInfo().(w, r) |
| 81 | case len(parts) == 3 && parts[0] == "ca" && parts[1] != "" && parts[2] == "certificates": |
| 82 | return a.handleCACertsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddypki`/adminAPI#handleCACerts().(w, r) |
| 83 | } |
| 84 | return caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.APIErrorscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/APIError#{ |
| 85 | HTTPStatusscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/APIError#HTTPStatus.: httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.StatusNotFoundscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/StatusNotFound., |
| 86 | Errscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/APIError#Err.: fmtscip-go gomod github.com/golang/go/src go1.25.1 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.1 fmt/Errorf().("resource not found: %v", r.URLscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#URL..Pathscip-go gomod github.com/golang/go/src go1.25.1 `net/url`/URL#Path.), |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | // handleCAInfo returns information about a particular |
| 91 | // CA by its ID. If the CA ID is the default, then the CA will be |
| 92 | // provisioned if it has not already been. Other CA IDs will return an |
| 93 | // error if they have not been previously provisioned. |
| 94 | func (a *adminAPIscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddypki`/adminAPI#) handleCAInfoscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddypki`/adminAPI#handleCAInfo().(w httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.ResponseWriterscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/ResponseWriter#, r *httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.Requestscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#) error { |
| 95 | if r.Methodscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#Method. != httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.MethodGetscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/MethodGet. { |
| 96 | return caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.APIErrorscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/APIError#{ |
| 97 | HTTPStatusscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/APIError#HTTPStatus.: httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.StatusMethodNotAllowedscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/StatusMethodNotAllowed., |
| 98 | Errscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/APIError#Err.: fmtscip-go gomod github.com/golang/go/src go1.25.1 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.1 fmt/Errorf().("method not allowed: %v", r.Methodscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#Method.), |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | ca, err := a.getCAFromAPIRequestPathscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddypki`/adminAPI#getCAFromAPIRequestPath().(r) |
| 103 | if err != nil { |
| 104 | return err |
| 105 | } |
| 106 | |
| 107 | rootCert, interCert, err := rootAndIntermediatePEMscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddypki`/rootAndIntermediatePEM().(ca) |
| 108 | if err != nil { |
| 109 | return caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.APIErrorscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/APIError#{ |
| 110 | HTTPStatusscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/APIError#HTTPStatus.: httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.StatusInternalServerErrorscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/StatusInternalServerError., |
| 111 | Errscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/APIError#Err.: fmtscip-go gomod github.com/golang/go/src go1.25.1 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.1 fmt/Errorf().("failed to get root and intermediate cert for CA %s: %v", ca.IDscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddypki`/CA#ID., err), |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | repl := ca.newReplacerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddypki`/CA#newReplacer().() |
| 116 | |
| 117 | response := caInfoscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddypki`/caInfo#{ |
| 118 | IDscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddypki`/caInfo#ID.: ca.IDscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddypki`/CA#ID., |
| 119 | Namescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddypki`/caInfo#Name.: ca.Namescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddypki`/CA#Name., |
| 120 | RootCNscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddypki`/caInfo#RootCN.: repl.ReplaceAllscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/Replacer#ReplaceAll().(ca.RootCommonNamescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddypki`/CA#RootCommonName., ""), |
| 121 | IntermediateCNscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddypki`/caInfo#IntermediateCN.: repl.ReplaceAllscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/Replacer#ReplaceAll().(ca.IntermediateCommonNamescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddypki`/CA#IntermediateCommonName., ""), |
| 122 | RootCertscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddypki`/caInfo#RootCert.: string(rootCert), |
| 123 | IntermediateCertscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddypki`/caInfo#IntermediateCert.: string(interCert), |
| 124 | } |
| 125 | |
| 126 | encoded, err := jsonscip-go gomod github.com/golang/go/src go1.25.1 `encoding/json`/.Marshalscip-go gomod github.com/golang/go/src go1.25.1 `encoding/json`/Marshal().(response) |
| 127 | if err != nil { |
| 128 | return caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.APIErrorscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/APIError#{ |
| 129 | HTTPStatusscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/APIError#HTTPStatus.: httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.StatusInternalServerErrorscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/StatusInternalServerError., |
| 130 | Errscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/APIError#Err.: err, |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | w.Headerscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/ResponseWriter#Header().().Setscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Header#Set().("Content-Type", "application/json") |
| 135 | _, _ = w.Writescip-go gomod github.com/golang/go/src go1.25.1 `net/http`/ResponseWriter#Write().(encoded) |
| 136 | |
| 137 | return nil |
| 138 | } |
| 139 | |
| 140 | // handleCACerts returns the certificate chain for a particular |
| 141 | // CA by its ID. If the CA ID is the default, then the CA will be |
| 142 | // provisioned if it has not already been. Other CA IDs will return an |
| 143 | // error if they have not been previously provisioned. |
| 144 | func (a *adminAPIscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddypki`/adminAPI#) handleCACertsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddypki`/adminAPI#handleCACerts().(w httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.ResponseWriterscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/ResponseWriter#, r *httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.Requestscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#) error { |
| 145 | if r.Methodscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#Method. != httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.MethodGetscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/MethodGet. { |
| 146 | return caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.APIErrorscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/APIError#{ |
| 147 | HTTPStatusscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/APIError#HTTPStatus.: httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.StatusMethodNotAllowedscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/StatusMethodNotAllowed., |
| 148 | Errscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/APIError#Err.: fmtscip-go gomod github.com/golang/go/src go1.25.1 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.1 fmt/Errorf().("method not allowed: %v", r.Methodscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#Method.), |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | ca, err := a.getCAFromAPIRequestPathscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddypki`/adminAPI#getCAFromAPIRequestPath().(r) |
| 153 | if err != nil { |
| 154 | return err |
| 155 | } |
| 156 | |
| 157 | rootCert, interCert, err := rootAndIntermediatePEMscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddypki`/rootAndIntermediatePEM().(ca) |
| 158 | if err != nil { |
| 159 | return caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.APIErrorscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/APIError#{ |
| 160 | HTTPStatusscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/APIError#HTTPStatus.: httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.StatusInternalServerErrorscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/StatusInternalServerError., |
| 161 | Errscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/APIError#Err.: fmtscip-go gomod github.com/golang/go/src go1.25.1 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.1 fmt/Errorf().("failed to get root and intermediate cert for CA %s: %v", ca.IDscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddypki`/CA#ID., err), |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | w.Headerscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/ResponseWriter#Header().().Setscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Header#Set().("Content-Type", "application/pem-certificate-chain") |
| 166 | _, err = w.Writescip-go gomod github.com/golang/go/src go1.25.1 `net/http`/ResponseWriter#Write().(interCert) //nolint:gosec // false positive... no XSS in a PEM for cryin' out loud |
| 167 | if err == nil { |
| 168 | _, _ = w.Writescip-go gomod github.com/golang/go/src go1.25.1 `net/http`/ResponseWriter#Write().(rootCert) //nolint:gosec // false positive... no XSS in a PEM for cryin' out loud |
| 169 | } |
| 170 | |
| 171 | return nil |
| 172 | } |
| 173 | |
| 174 | func (a *adminAPIscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddypki`/adminAPI#) getCAFromAPIRequestPathscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddypki`/adminAPI#getCAFromAPIRequestPath().(r *httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.Requestscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#) (*CAscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddypki`/CA#, error) { |
| 175 | // Grab the CA ID from the request path, it should be the 4th segment (/pki/ca/<ca>) |
| 176 | id := stringsscip-go gomod github.com/golang/go/src go1.25.1 strings/.Splitscip-go gomod github.com/golang/go/src go1.25.1 strings/Split().(r.URLscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/Request#URL..Pathscip-go gomod github.com/golang/go/src go1.25.1 `net/url`/URL#Path., "/")[3] |
| 177 | if id == "" { |
| 178 | return nil, caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.APIErrorscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/APIError#{ |
| 179 | HTTPStatusscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/APIError#HTTPStatus.: httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.StatusBadRequestscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/StatusBadRequest., |
| 180 | Errscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/APIError#Err.: fmtscip-go gomod github.com/golang/go/src go1.25.1 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.1 fmt/Errorf().("missing CA in path"), |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | // Find the CA by ID, if PKI is configured |
| 185 | var ca *CAscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddypki`/CA# |
| 186 | var ok bool |
| 187 | if a.pkiAppscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddypki`/adminAPI#pkiApp. != nil { |
| 188 | ca, ok = a.pkiAppscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddypki`/adminAPI#pkiApp..CAsscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddypki`/PKI#CAs.[id] |
| 189 | } |
| 190 | |
| 191 | // If we didn't find the CA, and PKI is not configured |
| 192 | // then we'll either error out if the CA ID is not the |
| 193 | // default. If the CA ID is the default, then we'll |
| 194 | // provision it, because the user probably aims to |
| 195 | // change their config to enable PKI immediately after |
| 196 | // if they actually requested the local CA ID. |
| 197 | if !ok { |
| 198 | if id != DefaultCAIDscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddypki`/DefaultCAID. { |
| 199 | return nil, caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.APIErrorscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/APIError#{ |
| 200 | HTTPStatusscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/APIError#HTTPStatus.: httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.StatusNotFoundscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/StatusNotFound., |
| 201 | Errscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/APIError#Err.: fmtscip-go gomod github.com/golang/go/src go1.25.1 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.1 fmt/Errorf().("no certificate authority configured with id: %s", id), |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | // Provision the default CA, which generates and stores a root |
| 206 | // certificate in storage, if one doesn't already exist. |
| 207 | ca = new(CAscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddypki`/CA#) |
| 208 | err := ca.Provisionscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddypki`/CA#Provision().(a.ctxscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddypki`/adminAPI#ctx., id, a.logscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddypki`/adminAPI#log.) |
| 209 | if err != nil { |
| 210 | return nil, caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.APIErrorscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/APIError#{ |
| 211 | HTTPStatusscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/APIError#HTTPStatus.: httpscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/.StatusInternalServerErrorscip-go gomod github.com/golang/go/src go1.25.1 `net/http`/StatusInternalServerError., |
| 212 | Errscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/APIError#Err.: fmtscip-go gomod github.com/golang/go/src go1.25.1 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.1 fmt/Errorf().("failed to provision CA %s, %w", id, err), |
| 213 | } |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | return ca, nil |
| 218 | } |
| 219 | |
| 220 | func rootAndIntermediatePEMscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddypki`/rootAndIntermediatePEM().(ca *CAscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddypki`/CA#) (root, inter []byte, err error) { |
| 221 | root, err = pemEncodeCertscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddypki`/pemEncodeCert().(ca.RootCertificatescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddypki`/CA#RootCertificate().().Rawscip-go gomod github.com/golang/go/src go1.25.1 `crypto/x509`/Certificate#Raw.) |
| 222 | if err != nil { |
| 223 | return root, inter, err |
| 224 | } |
| 225 | |
| 226 | for _, interCert := range ca.IntermediateCertificateChainscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddypki`/CA#IntermediateCertificateChain().() { |
| 227 | pemBytes, err := pemEncodeCertscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddypki`/pemEncodeCert().(interCert.Rawscip-go gomod github.com/golang/go/src go1.25.1 `crypto/x509`/Certificate#Raw.) |
| 228 | if err != nil { |
| 229 | return nil, nil, err |
| 230 | } |
| 231 | inter = append(inter, pemBytes...) |
| 232 | } |
| 233 | |
| 234 | return |
| 235 | } |
| 236 | |
| 237 | // caInfo is the response structure for the CA info API endpoint. |
| 238 | type caInfoscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddypki`/caInfo# struct { |
| 239 | IDscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddypki`/caInfo#ID. string `json:"id"` |
| 240 | Namescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddypki`/caInfo#Name. string `json:"name"` |
| 241 | RootCNscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddypki`/caInfo#RootCN. string `json:"root_common_name"` |
| 242 | IntermediateCNscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddypki`/caInfo#IntermediateCN. string `json:"intermediate_common_name"` |
| 243 | RootCertscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddypki`/caInfo#RootCert. string `json:"root_certificate"` |
| 244 | IntermediateCertscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddypki`/caInfo#IntermediateCert. string `json:"intermediate_certificate"` |
| 245 | } |
| 246 | |
| 247 | // adminPKIEndpointBase is the base admin endpoint under which all PKI admin endpoints exist. |
| 248 | const adminPKIEndpointBasescip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddypki`/adminPKIEndpointBase. = "/pki/" |
| 249 | |
| 250 | // Interface guards |
| 251 | var ( |
| 252 | _ caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.AdminRouterscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/AdminRouter# = (*adminAPIscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddypki`/adminAPI#)(nil) |
| 253 | _ caddyscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/.Provisionerscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2`/Provisioner# = (*adminAPIscip-go gomod github.com/caddyserver/caddy/v2 . `github.com/caddyserver/caddy/v2/modules/caddypki`/adminAPI#)(nil) |
| 254 | ) |