2022-07-25 19:17:52 +02:00
|
|
|
package controller
|
|
|
|
|
|
|
|
import (
|
2022-09-20 20:05:27 +02:00
|
|
|
"crypto/sha512"
|
2022-07-27 19:38:35 +02:00
|
|
|
"crypto/x509"
|
2022-07-25 19:17:52 +02:00
|
|
|
"encoding/hex"
|
2022-07-27 20:50:46 +02:00
|
|
|
errors2 "github.com/go-openapi/errors"
|
2022-07-27 20:45:16 +02:00
|
|
|
"github.com/openziti-test-kitchen/zrok/rest_model_zrok"
|
2022-07-27 19:38:35 +02:00
|
|
|
"github.com/openziti/edge/rest_management_api_client"
|
|
|
|
"github.com/openziti/edge/rest_util"
|
2022-10-18 21:21:53 +02:00
|
|
|
"github.com/teris-io/shortid"
|
2022-09-26 22:21:49 +02:00
|
|
|
"net/http"
|
|
|
|
"strings"
|
2022-07-25 19:17:52 +02:00
|
|
|
)
|
|
|
|
|
2022-07-27 20:45:16 +02:00
|
|
|
func ZrokAuthenticate(token string) (*rest_model_zrok.Principal, error) {
|
2022-07-29 21:28:40 +02:00
|
|
|
tx, err := str.Begin()
|
2022-07-27 20:45:16 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-07-29 21:54:13 +02:00
|
|
|
defer func() { _ = tx.Rollback() }()
|
2022-07-29 21:28:40 +02:00
|
|
|
if a, err := str.FindAccountWithToken(token, tx); err == nil {
|
2022-07-28 18:12:50 +02:00
|
|
|
principal := rest_model_zrok.Principal{
|
2022-09-09 16:20:05 +02:00
|
|
|
ID: int64(a.Id),
|
|
|
|
Token: a.Token,
|
|
|
|
Email: a.Email,
|
2022-07-28 18:12:50 +02:00
|
|
|
}
|
2022-07-27 20:45:16 +02:00
|
|
|
return &principal, nil
|
|
|
|
} else {
|
2022-07-27 20:50:46 +02:00
|
|
|
return nil, errors2.New(401, "invalid api key")
|
2022-07-27 20:45:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-12 17:03:15 +02:00
|
|
|
func edgeClient(cfg *ZitiConfig) (*rest_management_api_client.ZitiEdgeManagement, error) {
|
|
|
|
caCerts, err := rest_util.GetControllerWellKnownCas(cfg.ApiEndpoint)
|
2022-07-27 19:38:35 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
caPool := x509.NewCertPool()
|
|
|
|
for _, ca := range caCerts {
|
|
|
|
caPool.AddCert(ca)
|
|
|
|
}
|
2022-08-12 17:03:15 +02:00
|
|
|
return rest_util.NewEdgeManagementClientWithUpdb(cfg.Username, cfg.Password, cfg.ApiEndpoint, caPool)
|
2022-07-27 19:38:35 +02:00
|
|
|
}
|
|
|
|
|
2022-10-18 21:21:53 +02:00
|
|
|
func createToken() (string, error) {
|
|
|
|
return shortid.Generate()
|
2022-07-25 19:17:52 +02:00
|
|
|
}
|
2022-07-26 18:26:58 +02:00
|
|
|
|
2022-09-14 20:16:37 +02:00
|
|
|
func createServiceName() (string, error) {
|
2022-10-18 21:49:30 +02:00
|
|
|
return dnsSafeShortId()
|
|
|
|
}
|
|
|
|
|
|
|
|
func dnsSafeShortId() (string, error) {
|
|
|
|
sid, err := shortid.Generate()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
for sid[0] == '-' || sid[0] == '_' {
|
|
|
|
sid, err = shortid.Generate()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return sid, nil
|
2022-07-26 18:26:58 +02:00
|
|
|
}
|
2022-09-20 20:05:27 +02:00
|
|
|
|
|
|
|
func hashPassword(raw string) string {
|
|
|
|
hash := sha512.New()
|
|
|
|
hash.Write([]byte(raw))
|
|
|
|
return hex.EncodeToString(hash.Sum(nil))
|
|
|
|
}
|
2022-09-26 22:21:49 +02:00
|
|
|
|
|
|
|
func realRemoteAddress(req *http.Request) string {
|
|
|
|
ip := strings.Split(req.RemoteAddr, ":")[0]
|
|
|
|
fwdAddress := req.Header.Get("X-Forwarded-For")
|
|
|
|
if fwdAddress != "" {
|
|
|
|
ip = fwdAddress
|
|
|
|
|
|
|
|
ips := strings.Split(fwdAddress, ", ")
|
|
|
|
if len(ips) > 1 {
|
|
|
|
ip = ips[0]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ip
|
|
|
|
}
|