Skip to content
Merged
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
25 changes: 14 additions & 11 deletions blueprints/cis/cis_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

package cis

import "github.com/TsekNet/converge/dsl"
import (
"github.com/TsekNet/converge/condition"
"github.com/TsekNet/converge/dsl"
)

// DarwinCIS enforces CIS macOS L1 benchmark settings.
// Based on CIS Apple macOS 26 Tahoe v1.0.0 L1 (90 items).
Expand Down Expand Up @@ -40,12 +43,12 @@ func cisFirewall(r *dsl.Run) {
r.Exec("enable-firewall", dsl.ExecOpts{
Command: "/usr/libexec/ApplicationFirewall/socketfilterfw",
Args: []string{"--setglobalstate", "on"},
OnlyIf: "/usr/libexec/ApplicationFirewall/socketfilterfw --getglobalstate | grep -q enabled",
Condition: condition.Shell("/usr/libexec/ApplicationFirewall/socketfilterfw --getglobalstate | grep -q enabled"),
})
r.Exec("enable-stealth", dsl.ExecOpts{
Command: "/usr/libexec/ApplicationFirewall/socketfilterfw",
Args: []string{"--setstealthmode", "on"},
OnlyIf: "/usr/libexec/ApplicationFirewall/socketfilterfw --getstealthmode | grep -q enabled",
Condition: condition.Shell("/usr/libexec/ApplicationFirewall/socketfilterfw --getstealthmode | grep -q enabled"),
})
}

Expand All @@ -66,12 +69,12 @@ func cisSharing(r *dsl.Run) {
r.Exec("disable-remote-login", dsl.ExecOpts{
Command: "launchctl",
Args: []string{"disable", "system/com.apple.sshd"},
OnlyIf: "launchctl print system/com.apple.sshd 2>/dev/null | grep -q 'state = disabled'",
Condition: condition.Shell("launchctl print system/com.apple.sshd 2>/dev/null | grep -q 'state = disabled'"),
})
r.Exec("disable-remote-management", dsl.ExecOpts{
Command: "launchctl",
Args: []string{"disable", "system/com.apple.remotemanagement"},
OnlyIf: "launchctl print system/com.apple.remotemanagement 2>/dev/null | grep -q 'state = disabled'",
Condition: condition.Shell("launchctl print system/com.apple.remotemanagement 2>/dev/null | grep -q 'state = disabled'"),
})
r.Exec("disable-remote-apple-events", dsl.ExecOpts{
Command: "launchctl",
Expand All @@ -84,7 +87,7 @@ func cisSharing(r *dsl.Run) {
r.Exec("enable-time-service", dsl.ExecOpts{
Command: "systemsetup",
Args: []string{"-setusingnetworktime", "on"},
OnlyIf: "systemsetup -getusingnetworktime | grep -q 'On'",
Condition: condition.Shell("systemsetup -getusingnetworktime | grep -q 'On'"),
})
}

Expand Down Expand Up @@ -126,18 +129,18 @@ func cisSecurity(r *dsl.Run) {
r.Exec("enable-gatekeeper", dsl.ExecOpts{
Command: "spctl",
Args: []string{"--master-enable"},
OnlyIf: "spctl --status 2>&1 | grep -q enabled",
Condition: condition.Shell("spctl --status 2>&1 | grep -q enabled"),
})
r.Exec("check-filevault", dsl.ExecOpts{
Command: "fdesetup",
Args: []string{"status"},
OnlyIf: "fdesetup status | grep -q 'FileVault is On'",
Condition: condition.Shell("fdesetup status | grep -q 'FileVault is On'"),
})
// CIS 2.6.8: require admin password for system-wide preference changes
r.Exec("require-admin-for-system-prefs", dsl.ExecOpts{
Command: "security",
Args: []string{"authorizationdb", "write", "system.preferences", "authenticate-admin"},
OnlyIf: "security authorizationdb read system.preferences 2>/dev/null | grep -q authenticate-admin",
Condition: condition.Shell("security authorizationdb read system.preferences 2>/dev/null | grep -q authenticate-admin"),
})
// CIS 2.13.x: disable guest account and guest access to shares
r.Plist("com.apple.loginwindow", dsl.PlistOpts{Key: "GuestEnabled", Value: false, Type: "bool", Host: true})
Expand Down Expand Up @@ -165,12 +168,12 @@ func cisMisc(r *dsl.Run) {
r.Exec("disable-powernap", dsl.ExecOpts{
Command: "pmset",
Args: []string{"-a", "powernap", "0"},
OnlyIf: "pmset -g | grep -q 'powernap.*0'",
Condition: condition.Shell("pmset -g | grep -q 'powernap.*0'"),
})
r.Exec("disable-wake-network", dsl.ExecOpts{
Command: "pmset",
Args: []string{"-a", "womp", "0"},
OnlyIf: "pmset -g | grep -q 'womp.*0'",
Condition: condition.Shell("pmset -g | grep -q 'womp.*0'"),
})
r.Plist("com.apple.NetworkBrowser", dsl.PlistOpts{Key: "DisableAirDrop", Value: true, Type: "bool", Host: true})
r.Plist("com.apple.controlcenter", dsl.PlistOpts{Key: "AirplayRecieverEnabled", Value: false, Type: "bool", Host: true})
Expand Down
4 changes: 2 additions & 2 deletions blueprints/linux_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ func LinuxServer(r *dsl.Run) {
"X11Forwarding no\n" +
"MaxAuthTries 3\n",
Mode: 0600,
Meta: dsl.Meta{Critical: true},
Critical: true,
})

r.Service("sshd", dsl.ServiceOpts{
State: dsl.Running,
Enable: true,
Meta: dsl.Meta{Critical: true},
Critical: true,
})

r.Package("fail2ban", dsl.PackageOpts{State: dsl.Present})
Expand Down
216 changes: 216 additions & 0 deletions condition/combinators.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
package condition

import (
"context"
"fmt"
"strings"
"time"

"github.com/TsekNet/converge/extensions"
)

// All returns a Condition that is met when every sub-condition is met.
// Wait blocks until all sub-conditions are met (checked sequentially).
func All(conditions ...extensions.Condition) extensions.Condition {
return &allCondition{conditions: conditions}
}

// Any returns a Condition that is met when at least one sub-condition is met.
func Any(conditions ...extensions.Condition) extensions.Condition {
return &anyCondition{conditions: conditions}
}

// Not returns a Condition that inverts another: met when inner is not met.
func Not(inner extensions.Condition) extensions.Condition {
return &notCondition{inner: inner}
}

// ResourceIDs extracts all resource dependency IDs from a Condition tree.
// Used by the DSL to convert condition.Resource entries into DAG edges.
func ResourceIDs(c extensions.Condition) []string {
var ids []string
collectResourceIDs(c, &ids)
return ids
}

// StripResources returns a new Condition with all Resource conditions removed.
// If only Resource conditions remain, returns nil (no runtime condition needed).
func StripResources(c extensions.Condition) extensions.Condition {
return stripResources(c)
}

// --- allCondition ---

type allCondition struct {
conditions []extensions.Condition
}

func (c *allCondition) Met(ctx context.Context) (bool, error) {
for _, sub := range c.conditions {
met, err := sub.Met(ctx)
if err != nil {
return false, err
}
if !met {
return false, nil
}
}
return true, nil
}

func (c *allCondition) Wait(ctx context.Context) error {
for _, sub := range c.conditions {
if err := sub.Wait(ctx); err != nil {
return err
}
}
return nil
}

func (c *allCondition) String() string {
parts := make([]string, len(c.conditions))
for i, sub := range c.conditions {
parts[i] = sub.String()
}
return fmt.Sprintf("all(%s)", strings.Join(parts, ", "))
}

// --- anyCondition ---

type anyCondition struct {
conditions []extensions.Condition
}

func (c *anyCondition) Met(ctx context.Context) (bool, error) {
for _, sub := range c.conditions {
met, err := sub.Met(ctx)
if err != nil {
return false, err
}
if met {
return true, nil
}
}
return false, nil
}

func (c *anyCondition) Wait(ctx context.Context) error {
// Poll until any sub-condition is met.
ticker := time.NewTicker(2 * time.Second)
defer ticker.Stop()
for {
for _, sub := range c.conditions {
if met, _ := sub.Met(ctx); met {
return nil
}
}
select {
case <-ctx.Done():
return ctx.Err()
case <-ticker.C:
}
}
}

func (c *anyCondition) String() string {
parts := make([]string, len(c.conditions))
for i, sub := range c.conditions {
parts[i] = sub.String()
}
return fmt.Sprintf("any(%s)", strings.Join(parts, ", "))
}

// --- notCondition ---

type notCondition struct {
inner extensions.Condition
}

func (c *notCondition) Met(ctx context.Context) (bool, error) {
met, err := c.inner.Met(ctx)
if err != nil {
return false, err
}
return !met, nil
}

func (c *notCondition) Wait(ctx context.Context) error {
// Poll until the inner condition is NOT met.
ticker := time.NewTicker(2 * time.Second)
defer ticker.Stop()
for {
if met, _ := c.inner.Met(ctx); !met {
return nil
}
select {
case <-ctx.Done():
return ctx.Err()
case <-ticker.C:
}
}
}

func (c *notCondition) String() string {
return fmt.Sprintf("not(%s)", c.inner.String())
}

// --- tree helpers ---

func collectResourceIDs(c extensions.Condition, ids *[]string) {
switch v := c.(type) {
case *resourceCondition:
*ids = append(*ids, v.id)
case *allCondition:
for _, sub := range v.conditions {
collectResourceIDs(sub, ids)
}
case *anyCondition:
for _, sub := range v.conditions {
collectResourceIDs(sub, ids)
}
case *notCondition:
collectResourceIDs(v.inner, ids)
}
}

func stripResources(c extensions.Condition) extensions.Condition {
switch v := c.(type) {
case *resourceCondition:
return nil
case *allCondition:
var kept []extensions.Condition
for _, sub := range v.conditions {
if stripped := stripResources(sub); stripped != nil {
kept = append(kept, stripped)
}
}
if len(kept) == 0 {
return nil
}
if len(kept) == 1 {
return kept[0]
}
return &allCondition{conditions: kept}
case *anyCondition:
var kept []extensions.Condition
for _, sub := range v.conditions {
if stripped := stripResources(sub); stripped != nil {
kept = append(kept, stripped)
}
}
if len(kept) == 0 {
return nil
}
if len(kept) == 1 {
return kept[0]
}
return &anyCondition{conditions: kept}
case *notCondition:
if stripped := stripResources(v.inner); stripped != nil {
return &notCondition{inner: stripped}
}
return nil
default:
return c
}
}
Loading
Loading