Skip to content

Commit

Permalink
Merge branch 'unstable'
Browse files Browse the repository at this point in the history
  • Loading branch information
NHAS committed May 15, 2024
2 parents b98e3b9 + 2b10f0f commit 826863a
Show file tree
Hide file tree
Showing 49 changed files with 524 additions and 367 deletions.
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .idea/wag.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions commands/cleanup.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ package commands
import (
"flag"
"fmt"
"log"
"os"
"os/exec"

"github.com/NHAS/wag/internal/config"
"github.com/NHAS/wag/internal/data"
"github.com/NHAS/wag/internal/router"
"github.com/NHAS/wag/pkg/control/server"
"log"
"os"
)

type cleanup struct {
Expand Down Expand Up @@ -61,11 +60,12 @@ func (g *cleanup) Run() error {

if result != "0" && result != "3" {
log.Println("Cleaning up")

router.TearDown(true)
server.TearDown()
exec.Command("/usr/bin/wg-quick", "save", config.Values.Wireguard.DevName).Run()
data.TearDown()

return exec.Command("/usr/bin/wg-quick", "down", config.Values.Wireguard.DevName).Run()
return nil

}

Expand Down
12 changes: 7 additions & 5 deletions commands/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ func teardown(force bool) {

ui.Teardown()
webserver.Teardown()

}

func clusterState(noIptables bool, errorChan chan<- error) func(string) {
Expand All @@ -126,11 +125,14 @@ func clusterState(noIptables bool, errorChan chan<- error) func(string) {
switch stateText {
case "dead":
if !wasDead {
log.Println("Tearing down node")

teardown(false)

log.Println("Tear down complete")
if !config.Values.Clustering.Witness {
log.Println("Tearing down node")
teardown(false)
log.Println("Tear down complete")
} else {
log.Println("refusing to tear down witness node (nothing to tear down)")
}

// Only teardown if we were at one point alive
wasDead = true
Expand Down
2 changes: 1 addition & 1 deletion internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ func parseAddress(address string) ([]string, error) {
return nil, fmt.Errorf("no addresses for %s", address)
}

output := []string{}
var output []string
addedSomething := false
for _, addr := range addresses {
if addr.To4() != nil {
Expand Down
56 changes: 43 additions & 13 deletions internal/data/clustering.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ type NodeControlRequest struct {
Action string
}

func GetServerID() string {
return etcdServer.Server.ID().String()
func GetServerID() types.ID {
return etcdServer.Server.ID()
}

func GetLeader() types.ID {
Expand All @@ -49,11 +49,7 @@ func IsLearner() bool {
return etcdServer.Server.IsLearner()
}

func IsLeader() bool {
return etcdServer.Server.Leader() == etcdServer.Server.ID()
}

// Called on a leader node, to transfer ownership to another node (demoted)
// StepDown when called on a leader node, to transfer ownership to another node (demoted)
func StepDown() error {
return etcdServer.Server.TransferLeadership()
}
Expand All @@ -73,7 +69,7 @@ func GetLastPing(idHex string) (time.Time, error) {
return time.Time{}, errors.New("id is not part of cluster")
}

lastPing, err := etcd.Get(context.Background(), path.Join(NodeEvents, idHex, "ping"))
lastPing, err := etcd.Get(context.Background(), path.Join(NodeInfo, idHex, "ping"))
if err != nil {
return time.Time{}, err
}
Expand All @@ -94,18 +90,52 @@ func GetLastPing(idHex string) (time.Time, error) {
return t, nil
}

func SetDrained(idHex string, on bool) error {
func SetWitness(on bool) error {
if on {
_, err := etcd.Put(context.Background(), path.Join(NodeInfo, GetServerID().String(), "witness"), fmt.Sprintf("%t", on))
return err
}

_, err := etcd.Delete(context.Background(), path.Join(NodeInfo, GetServerID().String(), "witness"))
return err
}

func IsWitness(idHex string) (bool, error) {
_, err := strconv.ParseUint(idHex, 16, 64)
if err != nil {
return false, fmt.Errorf("bad member ID arg (%v), expecting ID in Hex", err)
}

isDrained, err := etcd.Get(context.Background(), path.Join(NodeInfo, idHex, "witness"))
if err != nil {
return false, err
}

return isDrained.Count != 0, nil
}

func SetDrained(idHex string, on bool) error {

isWitness, err := IsWitness(idHex)
if err != nil {
return err
}

if isWitness {
return errors.New("cannot set drained on witness node, this node is not serving clients")
}

_, err = strconv.ParseUint(idHex, 16, 64)
if err != nil {
return err
}

if on {
_, err = etcd.Put(context.Background(), path.Join(NodeEvents, idHex, "drain"), fmt.Sprintf("%t", on))
_, err = etcd.Put(context.Background(), path.Join(NodeInfo, idHex, "drain"), fmt.Sprintf("%t", on))
return err
}

_, err = etcd.Delete(context.Background(), path.Join(NodeEvents, idHex, "drain"))
_, err = etcd.Delete(context.Background(), path.Join(NodeInfo, idHex, "drain"))
return err
}

Expand All @@ -115,7 +145,7 @@ func IsDrained(idHex string) (bool, error) {
return false, fmt.Errorf("bad member ID arg (%v), expecting ID in Hex", err)
}

isDrained, err := etcd.Get(context.Background(), path.Join(NodeEvents, idHex, "drain"))
isDrained, err := etcd.Get(context.Background(), path.Join(NodeInfo, idHex, "drain"))
if err != nil {
return false, err
}
Expand Down Expand Up @@ -234,7 +264,7 @@ func RemoveMember(idHex string) error {
}

// Clear any node metadata
_, err = etcd.Delete(context.Background(), path.Join(NodeEvents, idHex), clientv3.WithPrefix())
_, err = etcd.Delete(context.Background(), path.Join(NodeInfo, idHex), clientv3.WithPrefix())
if err != nil {
return err
}
Expand Down
63 changes: 0 additions & 63 deletions internal/data/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"net/url"
"strings"

"github.com/NHAS/wag/internal/data/validators"
"github.com/go-playground/validator/v10"
clientv3 "go.etcd.io/etcd/client/v3"
)
Expand Down Expand Up @@ -92,16 +91,6 @@ func getInt(key string) (ret int, err error) {
return ret, nil
}

func SetPAM(details PAM) error {
d, err := json.Marshal(details)
if err != nil {
return err
}

_, err = etcd.Put(context.Background(), PamDetailsKey, string(d))
return err
}

func GetPAM() (details PAM, err error) {

response, err := etcd.Get(context.Background(), OidcDetailsKey)
Expand All @@ -117,16 +106,6 @@ func GetPAM() (details PAM, err error) {
return
}

func SetOidc(details OIDC) error {
d, err := json.Marshal(details)
if err != nil {
return err
}

_, err = etcd.Put(context.Background(), OidcDetailsKey, string(d))
return err
}

func GetOidc() (details OIDC, err error) {

response, err := etcd.Get(context.Background(), OidcDetailsKey)
Expand Down Expand Up @@ -175,13 +154,6 @@ func GetWebauthn() (wba Webauthn, err error) {
return
}

func SetWireguardConfigName(wgConfig string) error {
data, _ := json.Marshal(wgConfig)

_, err := etcd.Put(context.Background(), defaultWGFileNameKey, string(data))
return err
}

func GetWireguardConfigName() string {
k, err := getString(defaultWGFileNameKey)
if err != nil {
Expand Down Expand Up @@ -232,14 +204,6 @@ func GetAuthenicationMethods() (result []string, err error) {
return
}

func SetCheckUpdates(doChecks bool) error {

data, _ := json.Marshal(doChecks)

_, err := etcd.Put(context.Background(), checkUpdatesKey, string(data))
return err
}

func ShouldCheckUpdates() (bool, error) {

resp, err := etcd.Get(context.Background(), checkUpdatesKey)
Expand All @@ -261,12 +225,6 @@ func ShouldCheckUpdates() (bool, error) {
return ret, nil
}

func SetDomain(domain string) error {
data, _ := json.Marshal(domain)
_, err := etcd.Put(context.Background(), DomainKey, string(data))
return err
}

func GetDomain() (string, error) {
return getString(DomainKey)
}
Expand Down Expand Up @@ -297,17 +255,6 @@ func GetHelpMail() string {
return mail
}

func SetExternalAddress(externalAddress string) error {

if err := validators.ValidExternalAddresses(externalAddress); err != nil {
return err
}

data, _ := json.Marshal(externalAddress)
_, err := etcd.Put(context.Background(), externalAddressKey, string(data))
return err
}

func GetExternalAddress() (string, error) {
return getString(externalAddressKey)
}
Expand Down Expand Up @@ -625,16 +572,6 @@ func GetSessionInactivityTimeoutMinutes() (int, error) {
return inactivityTimeout, nil
}

func SetLockout(accountLockout int) error {
if accountLockout < 1 {
return errors.New("cannot set lockout to be below 1 as all accounts would be locked out")
}

data, _ := json.Marshal(accountLockout)
_, err := etcd.Put(context.Background(), LockoutKey, string(data))
return err
}

// Get account lockout threshold setting
func GetLockout() (int, error) {
lockout, err := getInt(LockoutKey)
Expand Down
Loading

0 comments on commit 826863a

Please sign in to comment.