2021-10-13 03:44:19 +08:00
|
|
|
package logic
|
|
|
|
|
2021-10-14 03:15:20 +08:00
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
|
|
|
|
"github.com/gravitl/netmaker/database"
|
|
|
|
)
|
|
|
|
|
|
|
|
type serverData struct {
|
|
|
|
PrivateKey string `json:"privatekey,omitempty" bson:"privatekey,omitempty"`
|
|
|
|
}
|
2021-10-13 03:44:19 +08:00
|
|
|
|
|
|
|
// StorePrivKey - stores server client WireGuard privatekey if needed
|
|
|
|
func StorePrivKey(serverID string, privateKey string) error {
|
2021-12-11 04:01:10 +08:00
|
|
|
var newData = serverData{}
|
2021-10-14 03:15:20 +08:00
|
|
|
var err error
|
|
|
|
var data []byte
|
|
|
|
newData.PrivateKey = privateKey
|
2021-12-11 04:01:10 +08:00
|
|
|
data, err = json.Marshal(&newData)
|
2021-10-14 03:15:20 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return database.Insert(serverID, string(data), database.SERVERCONF_TABLE_NAME)
|
2021-10-13 03:44:19 +08:00
|
|
|
}
|
|
|
|
|
2021-10-14 03:15:20 +08:00
|
|
|
// FetchPrivKey - fetches private key
|
2021-10-13 03:44:19 +08:00
|
|
|
func FetchPrivKey(serverID string) (string, error) {
|
2021-10-14 03:15:20 +08:00
|
|
|
var dbData string
|
|
|
|
var err error
|
2021-12-11 04:01:10 +08:00
|
|
|
var fetchedData = serverData{}
|
2021-10-14 03:15:20 +08:00
|
|
|
dbData, err = database.FetchRecord(database.SERVERCONF_TABLE_NAME, serverID)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
err = json.Unmarshal([]byte(dbData), &fetchedData)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return fetchedData.PrivateKey, nil
|
2021-10-13 03:44:19 +08:00
|
|
|
}
|
|
|
|
|
2021-10-14 03:15:20 +08:00
|
|
|
// RemovePrivKey - removes a private key
|
2021-10-13 03:44:19 +08:00
|
|
|
func RemovePrivKey(serverID string) error {
|
|
|
|
return database.DeleteRecord(database.SERVERCONF_TABLE_NAME, serverID)
|
|
|
|
}
|
2022-02-15 22:51:21 +08:00
|
|
|
|
2022-02-15 22:54:36 +08:00
|
|
|
// FetchJWTSecret - fetches jwt secret from db
|
2022-02-15 22:51:21 +08:00
|
|
|
func FetchJWTSecret() (string, error) {
|
|
|
|
var dbData string
|
|
|
|
var err error
|
|
|
|
var fetchedData = serverData{}
|
|
|
|
dbData, err = database.FetchRecord(database.SERVERCONF_TABLE_NAME, "nm-jwt-secret")
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
err = json.Unmarshal([]byte(dbData), &fetchedData)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return fetchedData.PrivateKey, nil
|
|
|
|
}
|
|
|
|
|
2022-02-15 22:54:36 +08:00
|
|
|
// StoreJWTSecret - stores server jwt secret if needed
|
2022-02-15 22:51:21 +08:00
|
|
|
func StoreJWTSecret(privateKey string) error {
|
|
|
|
var newData = serverData{}
|
|
|
|
var err error
|
|
|
|
var data []byte
|
|
|
|
newData.PrivateKey = privateKey
|
|
|
|
data, err = json.Marshal(&newData)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return database.Insert("nm-jwt-secret", string(data), database.SERVERCONF_TABLE_NAME)
|
|
|
|
}
|