Skip to content

Commit

Permalink
Create directories after checking for binaries
Browse files Browse the repository at this point in the history
In case the binaries needed for certificate autoenrollment are not
present on the system, avoid creating the needed directories.

Also add a test to illustrate that we will not fail if the directory
structure is wonky, if certmonger is not present.
  • Loading branch information
GabrielNagy committed Aug 8, 2023
1 parent 29e4d76 commit ab81b95
Show file tree
Hide file tree
Showing 31 changed files with 86 additions and 20 deletions.
30 changes: 24 additions & 6 deletions cmd/adsysd/integration_tests/adsysctl_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ func TestPolicyUpdate(t *testing.T) {
readOnlyDirs []string
winbindMockBehavior string
purge bool
missingCertmonger bool

wantErr bool
}{
Expand Down Expand Up @@ -603,6 +604,21 @@ func TestPolicyUpdate(t *testing.T) {
initState: "localhost-uptodate",
systemAnswer: "no_proxy_object",
},
"Does not error when certmonger or cepces is not available": {
args: []string{"-m"},
krb5ccname: "-",
krb5ccNamesState: []krb5ccNamesWithState{
{
src: "ccache_EXAMPLE.COM",
machine: true,
},
},
initState: "localhost-uptodate",
addPaths: []string{
"lib/private", // make parent of private dir a file
},
missingCertmonger: true,
},

// Purge cases
"Purge current user policies": {
Expand Down Expand Up @@ -966,13 +982,15 @@ func TestPolicyUpdate(t *testing.T) {
t.Setenv("ADSYS_WBCLIENT_BEHAVIOR", tc.winbindMockBehavior)

// Create fake certmonger and cepces binaries for the certificate manager
binDir := t.TempDir()
for _, executable := range []string{"getcert", "cepces-submit"} {
// #nosec G306. We want this asset to be executable.
err := os.WriteFile(filepath.Join(binDir, executable), []byte("#!/bin/sh\necho $@\n"), 0755)
require.NoError(t, err, "Setup: could not create %q binary", executable)
if !tc.missingCertmonger {
binDir := t.TempDir()
for _, executable := range []string{"getcert", "cepces-submit"} {
// #nosec G306. We want this asset to be executable.
err := os.WriteFile(filepath.Join(binDir, executable), []byte("#!/bin/sh\necho $@\n"), 0755)
require.NoError(t, err, "Setup: could not create %q binary", executable)
}
t.Setenv("PATH", binDir+":"+os.Getenv("PATH"))
}
t.Setenv("PATH", binDir+":"+os.Getenv("PATH"))

// Some tests will need some initial state assets
for _, k := range tc.clearDirs {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/usr/bin/baz {}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/usr/bin/bar {}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/usr/bin/foo {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
^adsystestuser@example.com {
/etc/environment r,
@{HOMEDIRS}/.xauth* w,
/usr/bin/{,b,d,rb}ash Ux,
/usr/bin/{c,k,tc}sh Ux,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[org/gnome/desktop/interface]
clock-format='24h'
clock-show-date=false
clock-show-weekday=true
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/org/gnome/desktop/interface/clock-format
/org/gnome/desktop/interface/clock-show-date
/org/gnome/desktop/interface/clock-show-weekday
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
user-db:user
system-db:gdm
system-db:machine
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
new content
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
TDB file
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# This file is managed by adsys.
# Do not edit this file manually.
# Any changes will be overwritten.

[Configuration]
AdminIdentities=unix-user:bob@example.com;unix-group:mygroup@example2.com
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
final machine script
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
script user logon
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
script user logoff
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
script machine shutdown
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
script machine startup
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
script user logon
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
subfolder other script
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
unreferenced data
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
unreferenced script
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
scripts/script-machine-startup
scripts/subfolder/other-script
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# This file is managed by adsys.
# Do not edit this file manually.
# Any changes will be overwritten.

%admin ALL=(ALL) !ALL
%sudo ALL=(ALL:ALL) !ALL

"bob@example.com" ALL=(ALL:ALL) ALL
"%mygroup@example2.com" ALL=(ALL:ALL) ALL

24 changes: 12 additions & 12 deletions internal/policies/certificate/cert-autoenroll
Original file line number Diff line number Diff line change
Expand Up @@ -58,30 +58,30 @@ def main():
private_dir = os.path.join(args.state_dir, 'private', 'certs')
global_trust_dir = args.global_trust_dir

# Create needed directories if they don't exist
for directory in [samba_cache_dir, trust_dir, private_dir, global_trust_dir]:
if not os.path.exists(directory):
perms = 0o700 if directory == private_dir else 0o755
os.makedirs(directory, mode=perms)

with tempfile.NamedTemporaryFile(prefix='smb_conf') as smb_conf:
smb_conf.write(smb_config(args.realm, args.debug).encode('utf-8'))
smb_conf.flush()

lp = param.LoadParm(smb_conf.name)
c = Credentials()
c.set_kerberos_state(MUST_USE_KERBEROS)
c.guess(lp)
username = c.get_username()
store = GPOStorage(os.path.join(samba_cache_dir, f'cert_gpo_state_{args.object_name}.tdb'))

# Set up logging
logger_init('cert-autoenroll', lp.log_level())

if not cepces_submit() or not certmonger():
log.warning('certmonger and/or cepces not found, skipping certificate enrollment')
return

# Create needed directories if they don't exist
for directory in [samba_cache_dir, trust_dir, private_dir, global_trust_dir]:
if not os.path.exists(directory):
perms = 0o700 if directory == private_dir else 0o755
os.makedirs(directory, mode=perms)

c = Credentials()
c.set_kerberos_state(MUST_USE_KERBEROS)
c.guess(lp)
username = c.get_username()
store = GPOStorage(os.path.join(samba_cache_dir, f'cert_gpo_state_{args.object_name}.tdb'))

ext = adsys_cert_auto_enroll(lp, c, username, store)
guid = f'adsys-cert-autoenroll-{args.object_name}'
if args.action == 'enroll':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@ Loading smb.conf
[global]
realm = example.com

Loading state file: #STATEDIR#/samba/cert_gpo_state_keypress.tdb
WARNING: certmonger and/or cepces not found, skipping certificate enrollment
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@ Loading smb.conf
[global]
realm = example.com

Loading state file: #STATEDIR#/samba/cert_gpo_state_keypress.tdb
WARNING: certmonger and/or cepces not found, skipping certificate enrollment

0 comments on commit ab81b95

Please sign in to comment.