Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for more zone calls with a break out to a zone structure. #6

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 30 additions & 68 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,104 +34,66 @@ func NewConfigClient(conn connection) *ConfigClient {
}
}

const configGetZoneNamesMethod = "org.fedoraproject.FirewallD1.config.getZoneNames"
// FirewallD config prefix.
const firewalldConfig = "org.fedoraproject.FirewallD1.config."

func (c *ConfigClient) callWithReturn(ctx context.Context, method string, returnArg interface{}, args ...interface{}) error {
call := newCall(firewalldConfig+method, 0).WithArguments(args...).WithReturns(returnArg)
return c.configPath.Call(ctx, call)
}

// Return list of zone names (permanent configuration).
func (c *ConfigClient) GetZoneNames(
ctx context.Context) ([]string, error) {
var zoneNames []string
return zoneNames, c.configPath.Call(ctx,
newCall(configGetZoneNamesMethod, 0).
WithReturns(&zoneNames))
return zoneNames, c.callWithReturn(ctx, "getZoneNames", &zoneNames)
}

const configGetServiceNamesMethod = "org.fedoraproject.FirewallD1.config.getServiceNames"

// Return list of service names (permanent configuration).
func (c *ConfigClient) GetServiceNames(
ctx context.Context) ([]string, error) {
var serviceNames []string
return serviceNames, c.configPath.Call(ctx,
newCall(configGetServiceNamesMethod, 0).
WithReturns(&serviceNames))
return serviceNames, c.callWithReturn(ctx, "getServiceNames", &serviceNames)
}

const configListZonesMethod = "org.fedoraproject.FirewallD1.config.listZones"

// List object paths of zones known to permanent environment.
func (c *ConfigClient) ListZones(
ctx context.Context) (zonePaths []string, err error) {
return zonePaths, c.configPath.Call(ctx,
newCall(configListZonesMethod, 0).
WithReturns(&zonePaths))
return zonePaths, c.callWithReturn(ctx, "listZones", &zonePaths)
}

const configGetZoneByNameMethod = "org.fedoraproject.FirewallD1.config.getZoneByName"

// Return object path (permanent configuration) of zone with given name.
func (c *ConfigClient) GetZoneByName(
ctx context.Context, zoneName string) (zonePath string, err error) {
return zonePath, c.configPath.Call(ctx,
newCall(configGetZoneByNameMethod, 0).
WithArguments(zoneName).
WithReturns(&zonePath))
ctx context.Context, zoneName string) (zone *Zone, err error) {
var path string
err = c.callWithReturn(ctx, "getZoneByName", &path, zoneName)
if err != nil {
return
}
zone = new(Zone)
zone.Path = path
zone.configPath = c.conn.Object(dbusDest, path)
return
}

const configGetServiceByNameMethod = "org.fedoraproject.FirewallD1.config.getServiceByName"

// Return object path (permanent configuration) of service with given name.
func (c *ConfigClient) GetServiceByName(
ctx context.Context, serviceName string) (servicePath string, err error) {
return servicePath, c.configPath.Call(ctx,
newCall(configGetServiceByNameMethod, 0).
WithArguments(serviceName).
WithReturns(&servicePath))
}

// Zone instance object interface
const configZoneRemoveMethod = "org.fedoraproject.FirewallD1.config.zone.remove"

// Remove zone with given settings into permanent configuration.
func (c *ConfigClient) RemoveZone(
ctx context.Context, zoneName string) error {
path, err := c.GetZoneByName(ctx, zoneName)
if err != nil {
return err
}
return c.conn.Object(dbusDest, path).
Call(ctx, newCall(configZoneRemoveMethod, 0))
return servicePath, c.callWithReturn(ctx, "getServiceByName", &servicePath, serviceName)
}

const addZoneMethod = "org.fedoraproject.FirewallD1.config.addZone"

// Add zone with given settings into permanent configuration.
// DEPRECATED: Add zone with given settings into permanent configuration.
// Needs https://github.com/godbus/dbus/pull/329 before this can fully function.
func (c *ConfigClient) AddZone(
ctx context.Context, zoneName string, settings ZoneSettings) error {
var z interface{}
return c.configPath.Call(ctx,
newCall(addZoneMethod, 0).
WithArguments(zoneName, settings.ToSlice()).
WithReturns(&z))
return c.callWithReturn(ctx, "addZone", &z, zoneName, settings.ToSlice())
}

const configZoneGetSettingsMethod = "org.fedoraproject.FirewallD1.config.zone.getSettings"

// Return permanent settings of given zone.
func (c *ConfigClient) GetZoneSettings(
ctx context.Context, zoneName string) (ZoneSettings, error) {
path, err := c.GetZoneByName(ctx, zoneName)
if err != nil {
return ZoneSettings{}, err
}

var zoneSettings []interface{}
err = c.conn.Object(dbusDest, path).
Call(ctx,
newCall(configZoneGetSettingsMethod, 0).
WithReturns(&zoneSettings))
if err != nil {
return ZoneSettings{}, err
}

return ZoneSettingsFromSlice(zoneSettings), nil
// Add zone with given settings into permanent configuration.
// Needs https://github.com/godbus/dbus/pull/329 before this can fully function.
func (c *ConfigClient) AddZone2(
ctx context.Context, zoneName string, settings ZoneSettings) error {
var z interface{}
return c.callWithReturn(ctx, "addZone2", &z, zoneName, settings.ToMap2())
}
29 changes: 21 additions & 8 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,22 +109,29 @@ func TestConfigClient_ListZones(t *testing.T) {
func TestConfigClient_GetZoneByName(t *testing.T) {
response := "/org/fedoraproject/FirewallD1/config/zone/0"

configPathCaller, _, c := configClientSetup()
configPathCaller, conn, c := configClientSetup()
configPathCaller.
On("Call", mock.Anything, mock.Anything).
On("Call", mock.Anything, mock.MatchedBy(func(o interface{}) bool {
c := o.(call)
return c.Method == firewalldConfig+"getZoneByName"
})).
Run(func(args mock.Arguments) {
c := args.Get(1).(call)
s := c.Returns[0].(*string)
*s = response
}).
Return(nil)
zoneObjectCaller := &callerMock{}
conn.
On("Object", dbusDest, response).
Return(zoneObjectCaller)

ctx := context.Background()

zonePath, err := c.GetZoneByName(ctx, "test")
zone, err := c.GetZoneByName(ctx, "test")
require.NoError(t, err)

assert.Equal(t, response, zonePath)
assert.Equal(t, response, zone.Path)
}

func TestConfigClient_GetServiceByName(t *testing.T) {
Expand Down Expand Up @@ -155,7 +162,7 @@ func TestConfigClient_RemoveZone(t *testing.T) {
configPathCaller.
On("Call", mock.Anything, mock.MatchedBy(func(o interface{}) bool {
c := o.(call)
return c.Method == configGetZoneByNameMethod
return c.Method == firewalldConfig+"getZoneByName"
})).
Run(func(args mock.Arguments) {
c := args.Get(1).(call)
Expand All @@ -174,7 +181,10 @@ func TestConfigClient_RemoveZone(t *testing.T) {

ctx := context.Background()

err := c.RemoveZone(ctx, "ssh")
zone, err := c.GetZoneByName(ctx, "ssh")
require.NoError(t, err)

err = zone.Remove(ctx)
require.NoError(t, err)
}

Expand Down Expand Up @@ -249,7 +259,7 @@ func TestClient_GetZoneSettings(t *testing.T) {
configPathCaller.
On("Call", mock.Anything, mock.MatchedBy(func(o interface{}) bool {
c := o.(call)
return c.Method == configGetZoneByNameMethod
return c.Method == firewalldConfig+"getZoneByName"
})).
Run(func(args mock.Arguments) {
c := args.Get(1).(call)
Expand All @@ -273,7 +283,10 @@ func TestClient_GetZoneSettings(t *testing.T) {

ctx := context.Background()

settings, err := c.GetZoneSettings(ctx, "ssh")
zone, err := c.GetZoneByName(ctx, "ssh")
require.NoError(t, err)

settings, err := zone.GetSettings(ctx)
require.NoError(t, err)

assert.Equal(t, expected, settings)
Expand Down
7 changes: 5 additions & 2 deletions examples/zones/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func main() {
exitOnErr(err)
fmt.Println("Initial Zones:", zones)

err = client.Config().AddZone(ctx, "test", firewalld.ZoneSettings{
err = client.Config().AddZone2(ctx, "test", firewalld.ZoneSettings{
Target: "default",
})
exitOnErr(err)
Expand All @@ -45,7 +45,10 @@ func main() {
exitOnErr(err)
fmt.Println("Updated Zones:", zones)

err = client.Config().RemoveZone(ctx, "test")
zone, err := client.Config().GetZoneByName(ctx, "test")
exitOnErr(err)

err = zone.Remove(ctx)
exitOnErr(err)
fmt.Println("Removed zone 'test'")
}
Expand Down
Loading