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

Fix DCONF policy manager removing userdb on empty policy #1070

Merged
merged 2 commits into from
Aug 28, 2024
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
user-db:user
system-db:[email protected]
system-db:machine
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 @@

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:[email protected]
system-db:machine
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 @@

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 @@

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
user-db:user
system-db:[email protected]
system-db:machine
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,3 @@
user-db:user
system-db:[email protected]
system-db:machine
17 changes: 1 addition & 16 deletions internal/policies/dconf/dconf.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,27 +95,12 @@ func (m *Manager) ApplyPolicy(ctx context.Context, objectName string, isComputer
dbsPath := filepath.Join(dconfDir, "db")
dbPath := filepath.Join(dbsPath, objectName+".d")

if !isComputer && len(entries) > 0 {
if !isComputer {
if _, err := os.Stat(filepath.Join(dbsPath, "machine.d", "locks", "adsys")); err != nil {
return errors.New(gotext.Get("machine dconf database is required before generating a policy for an user. This one returns: %v", err))
}
}

// Only clean up user databases/profiles if there are no entries to apply.
// We don't clean up the machine database because we don't know if there's any user GPO depending on it.
if !isComputer && len(entries) == 0 {
if err := os.RemoveAll(dbPath); err != nil {
return errors.New(gotext.Get("can't remove user dconf database directory: %v", err))
}
if er := os.RemoveAll(filepath.Join(dbsPath, objectName)); er != nil {
return errors.New(gotext.Get("can't remove user dconf binary database: %v", err))
}
if err := os.RemoveAll(filepath.Join(profilesPath, objectName)); err != nil {
return errors.New(gotext.Get("can't remove user dconf profile: %v", err))
}
return nil
}

// Create profiles for users only
if !isComputer {
//nolint:gosec // G301 - Profile must be readable by everyone
Expand Down
25 changes: 11 additions & 14 deletions internal/policies/dconf/dconf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ func TestApplyPolicy(t *testing.T) {
"Update user disabled key with value": {entries: []entry.Entry{
{Key: "com/ubuntu/category/key-s", Value: "'onekey-s-othervalue'", Meta: "s"}},
existingDconfDir: "user-with-disabled-value"},
"User empty state, with existing machine policy": {entries: []entry.Entry{}, existingDconfDir: "existing-user"},
"User empty state": {entries: []entry.Entry{}, existingDconfDir: "-"},

// Machine cases
"First boot": {entries: []entry.Entry{
Expand All @@ -57,7 +55,10 @@ func TestApplyPolicy(t *testing.T) {
"Update machine disabled key with value": {entries: []entry.Entry{
{Key: "com/ubuntu/category/key-s", Value: "'onekey-s-othervalue'", Meta: "s"}},
isComputer: true, existingDconfDir: "machine-with-disabled-value"},
"Machine empty state": {entries: []entry.Entry{}, isComputer: true, existingDconfDir: "-"},

// We still need to create an empty database even if there is no policy, otherwise DCONF will block any writes
// due to missing database profile stack file.
"No policy still generates a valid db": {entries: nil},
didrocks marked this conversation as resolved.
Show resolved Hide resolved

"Multiple keys same category": {entries: []entry.Entry{
{Key: "com/ubuntu/category/key-s", Value: "'onekey-s-othervalue'", Meta: "s"},
Expand Down Expand Up @@ -143,19 +144,19 @@ func TestApplyPolicy(t *testing.T) {
}},

// Profiles tests
"Update existing correct profile stays unchanged": {
"Update existing correct profile stays unchanged": {entries: nil,
existingDconfDir: "existing-user"},
"Update existing correct profile with trailing spaces are removed": {
"Update existing correct profile with trailing spaces are removed": {entries: nil,
existingDconfDir: "existing-user-with-trailing-spaces"},
"Update existing profile without needed db append them": {
"Update existing profile without needed db append them": {entries: nil,
existingDconfDir: "existing-user-no-adsysdb"},
"Update existing profile without needed db, trailine lines are removed": {
"Update existing profile without needed db, trailine lines are removed": {entries: nil,
existingDconfDir: "existing-user-no-adsysdb-trailing-newlines"},
"Update existing profile with partial db append them without repetition": {
"Update existing profile with partial db append them without repetition": {entries: nil,
existingDconfDir: "existing-user-one-adsysdb-partial"},
"Update existing profile with wrong order appends them in correct order": {
"Update existing profile with wrong order appends them in correct order": {entries: nil,
existingDconfDir: "existing-user-one-adsysdb-reversed-end"},
"Update existing profile eliminates adsys DB repetitions": {
"Update existing profile eliminates adsys DB repetitions": {entries: nil,
existingDconfDir: "existing-user-adsysdb-repetitions"},

// non adsys content
Expand Down Expand Up @@ -194,10 +195,6 @@ func TestApplyPolicy(t *testing.T) {

dconfDir := t.TempDir()

if tc.entries == nil {
tc.entries = []entry.Entry{{Key: "com/ubuntu/category/key-s", Value: "'onekey-s-othervalue'", Meta: "s"}}
}

if tc.existingDconfDir == "" {
tc.existingDconfDir = "machine-base"
}
Expand Down
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:ubuntu
system-db:machine
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
[com/ubuntu/category]
key-s='onekey-s-othervalue'

Original file line number Diff line number Diff line change
@@ -1 +1 @@
/com/ubuntu/category/key-s

Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
[com/ubuntu/category]
key-s='onekey-s-othervalue'

Original file line number Diff line number Diff line change
@@ -1 +1 @@
/com/ubuntu/category/key-s

Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
[com/ubuntu/category]
key-s='onekey-s-othervalue'

Original file line number Diff line number Diff line change
@@ -1 +1 @@
/com/ubuntu/category/key-s

Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
[com/ubuntu/category]
key-s='onekey-s-othervalue'

Original file line number Diff line number Diff line change
@@ -1 +1 @@
/com/ubuntu/category/key-s

Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
[com/ubuntu/category]
key-s='onekey-s-othervalue'

Original file line number Diff line number Diff line change
@@ -1 +1 @@
/com/ubuntu/category/key-s

Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
[com/ubuntu/category]
key-s='onekey-s-othervalue'

Original file line number Diff line number Diff line change
@@ -1 +1 @@
/com/ubuntu/category/key-s

Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
[com/ubuntu/category]
key-s='onekey-s-othervalue'

Original file line number Diff line number Diff line change
@@ -1 +1 @@
/com/ubuntu/category/key-s

This file was deleted.

This file was deleted.

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 @@

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 @@

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 @@

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 @@

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 @@

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 @@

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
Loading