Skip to content

Commit

Permalink
Add OpenConnect Runner Config
Browse files Browse the repository at this point in the history
Signed-off-by: hwipl <33433250+hwipl@users.noreply.github.com>
  • Loading branch information
hwipl committed Aug 15, 2023
1 parent 0faf5c1 commit 7abffee
Show file tree
Hide file tree
Showing 2 changed files with 169 additions and 0 deletions.
92 changes: 92 additions & 0 deletions internal/ocrunner/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package ocrunner

import "strconv"

var (
// XMLProfile is the default AnyConnect Profile
XMLProfile = "/var/lib/oc-daemon/profile.xml"

// VPNCScript is the default vpnc-script
VPNCScript = "/usr/bin/oc-daemon-vpncscript"

// VPNDevice is the default vpn network device name
VPNDevice = "oc-daemon-tun0"

// PIDFile is the default file path of the PID file for openconnect
PIDFile = "/run/oc-daemon/openconnect.pid"

// PIDOwner is the default owner of the PID file
PIDOwner = ""

// PIDGroup is the default group of the PID file
PIDGroup = ""

// PIDPermissions are the default file permissions of the PID file
PIDPermissions = "0600"

// NoProxy specifies whether the no proxy flag is set in openconnect
NoProxy = true

// ExtraEnv are extra environment variables used by openconnect
ExtraEnv = []string{}

// ExtraArgs are extra command line arguments used by openconnect
ExtraArgs = []string{}
)

// Config is the configuration for an openconnect connection runner
type Config struct {
XMLProfile string
VPNCScript string
VPNDevice string

PIDFile string
PIDOwner string
PIDGroup string
PIDPermissions string

NoProxy bool
ExtraEnv []string
ExtraArgs []string
}

// Valid returns whether the openconnect configuration is valid
func (c *Config) Valid() bool {
if c == nil ||
c.XMLProfile == "" ||
c.VPNCScript == "" ||
c.VPNDevice == "" ||
c.PIDFile == "" ||
c.PIDPermissions == "" {

return false
}
if c.PIDPermissions != "" {
perm, err := strconv.ParseUint(c.PIDPermissions, 8, 32)
if err != nil {
return false
}
if perm > 0777 {
return false
}
}
return true
}

// NewConfig returns a new configuration for an openconnect connection runner
func NewConfig() *Config {
return &Config{
XMLProfile: XMLProfile,
VPNCScript: VPNCScript,
VPNDevice: VPNDevice,

PIDFile: PIDFile,
PIDOwner: PIDOwner,
PIDGroup: PIDGroup,
PIDPermissions: PIDPermissions,

NoProxy: NoProxy,
ExtraEnv: ExtraEnv,
ExtraArgs: ExtraArgs,
}
}
77 changes: 77 additions & 0 deletions internal/ocrunner/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package ocrunner

import (
"reflect"
"testing"
)

// TestConfigValid tests Valid of Config
func TestConfigValid(t *testing.T) {
// test invalid
for _, invalid := range []*Config{
nil,
{},
{
XMLProfile: "/test/profile",
VPNCScript: "/test/vpncscript",
VPNDevice: "test-device",
PIDFile: "/test/pid",
PIDPermissions: "invalid",
},
{
XMLProfile: "/test/profile",
VPNCScript: "/test/vpncscript",
VPNDevice: "test-device",
PIDFile: "/test/pid",
PIDPermissions: "1234",
},
} {
want := false
got := invalid.Valid()

if got != want {
t.Errorf("got %t, want %t for %v", got, want, invalid)
}
}

// test valid
for _, valid := range []*Config{
NewConfig(),
{
XMLProfile: "/test/profile",
VPNCScript: "/test/vpncscript",
VPNDevice: "test-device",
PIDFile: "/test/pid",
PIDPermissions: "777",
},
} {
want := true
got := valid.Valid()

if got != want {
t.Errorf("got %t, want %t for %v", got, want, valid)
}
}
}

// TestNewConfig tests NewConfig
func TestNewConfig(t *testing.T) {
want := &Config{
XMLProfile: XMLProfile,
VPNCScript: VPNCScript,
VPNDevice: VPNDevice,

PIDFile: PIDFile,
PIDOwner: PIDOwner,
PIDGroup: PIDGroup,
PIDPermissions: PIDPermissions,

NoProxy: NoProxy,
ExtraEnv: ExtraEnv,
ExtraArgs: ExtraArgs,
}
got := NewConfig()
if !reflect.DeepEqual(got, want) {
t.Errorf("got %v, want %v", got, want)
}
}

0 comments on commit 7abffee

Please sign in to comment.