2019-02-04 02:56:43 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
2023-02-15 07:49:34 +00:00
|
|
|
"net/url"
|
|
|
|
"strconv"
|
2019-02-04 02:56:43 +00:00
|
|
|
|
|
|
|
restful "github.com/emicklei/go-restful"
|
2021-11-14 14:28:40 +00:00
|
|
|
|
2023-02-15 07:49:34 +00:00
|
|
|
"novit.tech/direktil/local-server/pkg/mime"
|
2022-04-28 01:33:19 +00:00
|
|
|
"novit.tech/direktil/pkg/localconfig"
|
2019-02-04 02:56:43 +00:00
|
|
|
)
|
|
|
|
|
2023-02-12 10:58:26 +00:00
|
|
|
var clusterSecretKVs = []string{}
|
|
|
|
|
|
|
|
func newClusterSecretKV[T any](name string) KVSecrets[T] {
|
2023-02-13 17:07:10 +00:00
|
|
|
clusterSecretKVs = append(clusterSecretKVs, name)
|
|
|
|
return KVSecrets[T]{"clusters/" + name}
|
2023-02-12 10:58:26 +00:00
|
|
|
}
|
|
|
|
|
2019-02-04 02:56:43 +00:00
|
|
|
func wsListClusters(req *restful.Request, resp *restful.Response) {
|
|
|
|
cfg := wsReadConfig(resp)
|
|
|
|
if cfg == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
clusterNames := make([]string, len(cfg.Clusters))
|
|
|
|
for i, cluster := range cfg.Clusters {
|
|
|
|
clusterNames[i] = cluster.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
resp.WriteEntity(clusterNames)
|
|
|
|
}
|
|
|
|
|
|
|
|
func wsReadCluster(req *restful.Request, resp *restful.Response) (cluster *localconfig.Cluster) {
|
|
|
|
clusterName := req.PathParameter("cluster-name")
|
|
|
|
|
|
|
|
cfg := wsReadConfig(resp)
|
|
|
|
if cfg == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
cluster = cfg.Cluster(clusterName)
|
|
|
|
if cluster == nil {
|
2023-02-13 17:07:10 +00:00
|
|
|
wsNotFound(resp)
|
2019-02-04 02:56:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func wsCluster(req *restful.Request, resp *restful.Response) {
|
|
|
|
cluster := wsReadCluster(req, resp)
|
|
|
|
if cluster == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
resp.WriteEntity(cluster)
|
|
|
|
}
|
|
|
|
|
|
|
|
func wsClusterAddons(req *restful.Request, resp *restful.Response) {
|
|
|
|
cluster := wsReadCluster(req, resp)
|
|
|
|
if cluster == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(cluster.Addons) == 0 {
|
|
|
|
log.Printf("cluster %q has no addons defined", cluster.Name)
|
2023-02-13 17:07:10 +00:00
|
|
|
wsNotFound(resp)
|
2019-02-04 02:56:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-02-12 14:18:42 +00:00
|
|
|
cfg := wsReadConfig(resp)
|
|
|
|
if cfg == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-02-13 17:07:10 +00:00
|
|
|
sslCfg, err := sslConfigFromLocalConfig(cfg)
|
|
|
|
if err != nil {
|
|
|
|
wsError(resp, err)
|
|
|
|
return
|
|
|
|
}
|
2023-02-12 14:18:42 +00:00
|
|
|
|
|
|
|
wsRender(resp, sslCfg, cluster.Addons, cluster)
|
2019-02-04 02:56:43 +00:00
|
|
|
}
|
2019-04-13 09:36:58 +00:00
|
|
|
|
2020-04-22 15:36:04 +00:00
|
|
|
func wsClusterCACert(req *restful.Request, resp *restful.Response) {
|
2023-02-15 07:49:34 +00:00
|
|
|
clusterName := req.PathParameter("cluster-name")
|
|
|
|
caName := req.PathParameter("ca-name")
|
|
|
|
|
|
|
|
ca, found, err := clusterCAs.Get(clusterName + "/" + caName)
|
|
|
|
if err != nil {
|
|
|
|
wsError(resp, err)
|
2020-04-22 15:36:04 +00:00
|
|
|
return
|
|
|
|
}
|
2023-02-15 07:49:34 +00:00
|
|
|
if !found {
|
2023-02-13 17:07:10 +00:00
|
|
|
wsNotFound(resp)
|
2020-04-22 15:36:04 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-02-15 07:49:34 +00:00
|
|
|
resp.Header().Set("Content-Type", mime.CERT)
|
2020-04-22 15:36:04 +00:00
|
|
|
resp.Write(ca.Cert)
|
|
|
|
}
|
|
|
|
|
|
|
|
func wsClusterSignedCert(req *restful.Request, resp *restful.Response) {
|
2023-02-15 07:49:34 +00:00
|
|
|
clusterName := req.PathParameter("cluster-name")
|
|
|
|
caName := req.PathParameter("ca-name")
|
2021-11-14 14:28:40 +00:00
|
|
|
name := req.QueryParameter("name")
|
|
|
|
|
2023-02-15 07:49:34 +00:00
|
|
|
kc, found, err := clusterCASignedKeys.Get(clusterName + "/" + caName + "/" + name)
|
|
|
|
if err != nil {
|
|
|
|
wsError(resp, err)
|
2020-04-22 15:36:04 +00:00
|
|
|
return
|
|
|
|
}
|
2023-02-15 07:49:34 +00:00
|
|
|
if !found {
|
2023-02-13 17:07:10 +00:00
|
|
|
wsNotFound(resp)
|
2020-04-22 15:36:04 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-02-15 07:49:34 +00:00
|
|
|
resp.AddHeader("Content-Type", mime.CERT)
|
|
|
|
resp.AddHeader("Content-Disposition", "attachment; filename="+strconv.Quote(clusterName+"_"+caName+"_"+url.PathEscape(name)+".crt"))
|
2020-04-22 15:36:04 +00:00
|
|
|
resp.Write(kc.Cert)
|
|
|
|
}
|