Skip to content

Commit c0b4dad

Browse files
author
Soft-Serve
committed
feat: adding secrets compare feature
1 parent de7304d commit c0b4dad

5 files changed

Lines changed: 81 additions & 15 deletions

File tree

cmd/gitops/main.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/mxcd/gitops-cli/internal/k8s"
99
"github.com/mxcd/gitops-cli/internal/kubernetes"
1010
"github.com/mxcd/gitops-cli/internal/patch"
11+
"github.com/mxcd/gitops-cli/internal/secret"
1112
"github.com/mxcd/gitops-cli/internal/state"
1213
"github.com/mxcd/gitops-cli/internal/templating"
1314
"github.com/mxcd/gitops-cli/internal/util"
@@ -140,6 +141,15 @@ func main() {
140141
return templating.TestTemplating(c)
141142
},
142143
},
144+
{
145+
Name: "compare",
146+
Aliases: []string{"c"},
147+
Usage: "Compare two secrets",
148+
Action: func(c *cli.Context) error {
149+
initApplication(c)
150+
return secret.CompareCommand(c)
151+
},
152+
},
143153
},
144154
},
145155
{

internal/plan/plan.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func (p *Plan) Print(showUnchanged bool) {
4545
if !showUnchanged && item.Diff.Equal {
4646
continue
4747
}
48-
item.Diff.Print()
48+
item.Diff.Print(false)
4949
if i < len(p.Items)-1 {
5050
println("---")
5151
}

internal/secret/compare.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package secret
2+
3+
import (
4+
"errors"
5+
6+
"github.com/urfave/cli/v2"
7+
)
8+
9+
func CompareCommand(c *cli.Context) error {
10+
if c.Args().Len() != 2 {
11+
return errors.New("usage: gitops secrets compare <secret-path-1> <secret-path-2>")
12+
}
13+
14+
firstPath := c.Args().Get(0)
15+
secondPath := c.Args().Get(1)
16+
17+
firstSecret, err := FromPath(firstPath)
18+
if err != nil {
19+
return err
20+
}
21+
22+
secondSecret, err := FromPath(secondPath)
23+
if err != nil {
24+
return err
25+
}
26+
27+
diff := CompareSecrets(firstSecret, secondSecret)
28+
diff.Print(c.Bool("show-unchanged"))
29+
30+
return nil
31+
}

internal/secret/diff.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,14 @@ func CompareSecrets(oldSecret *Secret, newSecret *Secret) *SecretDiff {
175175
NewValue: value,
176176
Sensitive: true,
177177
})
178+
} else if value == oldSecret.Data[key] {
179+
diffEntries = append(diffEntries, SecretDiffEntry{
180+
Type: SecretDiffTypeUnchanged,
181+
Key: fmt.Sprintf("data.%s", key),
182+
OldValue: value,
183+
NewValue: value,
184+
Sensitive: true,
185+
})
178186
}
179187
}
180188

@@ -198,7 +206,15 @@ func CompareSecrets(oldSecret *Secret, newSecret *Secret) *SecretDiff {
198206
Entries: diffEntries,
199207
}
200208

201-
if len(diffEntries) > 0 {
209+
hasChanges := false
210+
for _, entry := range diffEntries {
211+
if entry.Type != SecretDiffTypeUnchanged {
212+
hasChanges = true
213+
break
214+
}
215+
}
216+
217+
if hasChanges {
202218
diff.Type = SecretDiffTypeChanged
203219
diff.Equal = false
204220
} else {
@@ -209,14 +225,17 @@ func CompareSecrets(oldSecret *Secret, newSecret *Secret) *SecretDiff {
209225
return &diff
210226
}
211227

212-
func (d *SecretDiff) Print() {
228+
func (d *SecretDiff) Print(showUnchanged bool) {
213229
combinedSecretName := d.Name
214230
if d.Namespace != "" {
215231
combinedSecretName = fmt.Sprintf("%s/%s", d.Namespace, d.Name)
216232
}
217233

218234
printDetailedChanges := func() {
219235
for _, entry := range d.Entries {
236+
if entry.Type == SecretDiffTypeUnchanged && !showUnchanged {
237+
continue
238+
}
220239
safeOldValue := entry.OldValue
221240
safeNewValue := entry.NewValue
222241
if entry.Sensitive && !util.GetCliContext().Bool("cleartext") {
@@ -230,6 +249,8 @@ func (d *SecretDiff) Print() {
230249
println(color.Ize(color.Red, fmt.Sprintf(" - %s: %s", entry.Key, safeOldValue)))
231250
case SecretDiffTypeChanged:
232251
println(color.Ize(color.Yellow, fmt.Sprintf(" ~ %s: %s => %s", entry.Key, safeOldValue, safeNewValue)))
252+
case SecretDiffTypeUnchanged:
253+
println(color.Ize(color.Gray, fmt.Sprintf(" = %s: %s", entry.Key, safeNewValue)))
233254
}
234255
}
235256
}

internal/secret/secret_test.go

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func TestSecretComparisonTarget(t *testing.T) {
6161
}
6262

6363
diff := CompareSecrets(a, b)
64-
diff.Print()
64+
diff.Print(false)
6565
assert.Equal(t, false, diff.Equal, "Secrets should not be equal")
6666
assert.Equal(t, SecretDiffTypeChanged, diff.Type, "Diff type should be changed")
6767
assert.Equal(t, 1, len(diff.Entries), "Diff should have 1 entry")
@@ -88,7 +88,7 @@ func TestSecretComparisonType(t *testing.T) {
8888
}
8989

9090
diff := CompareSecrets(a, b)
91-
diff.Print()
91+
diff.Print(false)
9292
assert.Equal(t, false, diff.Equal, "Secrets should not be equal")
9393
assert.Equal(t, SecretDiffTypeChanged, diff.Type, "Diff type should be changed")
9494
assert.Equal(t, 1, len(diff.Entries), "Diff should have 1 entry")
@@ -115,7 +115,7 @@ func TestSecretComparisonChangedName(t *testing.T) {
115115
}
116116

117117
diff := CompareSecrets(a, b)
118-
diff.Print()
118+
diff.Print(false)
119119
assert.Equal(t, false, diff.Equal, "Secrets should not be equal")
120120
assert.Equal(t, SecretDiffTypeChanged, diff.Type, "Diff type should be changed")
121121
assert.Equal(t, 1, len(diff.Entries), "Diff should have 1 entry")
@@ -143,7 +143,7 @@ func TestSecretComparisonChangedNamespace(t *testing.T) {
143143
}
144144

145145
diff := CompareSecrets(a, b)
146-
diff.Print()
146+
diff.Print(false)
147147
assert.Equal(t, false, diff.Equal, "Secrets should not be equal")
148148
assert.Equal(t, SecretDiffTypeChanged, diff.Type, "Diff type should be changed")
149149
assert.Equal(t, 1, len(diff.Entries), "Diff should have 1 entry")
@@ -174,7 +174,7 @@ func TestSecretComparisonAddData(t *testing.T) {
174174
}
175175

176176
diff := CompareSecrets(a, b)
177-
diff.Print()
177+
diff.Print(false)
178178
assert.Equal(t, false, diff.Equal, "Secrets should not be equal")
179179
assert.Equal(t, SecretDiffTypeChanged, diff.Type, "Diff type should be changed")
180180
assert.Equal(t, 2, len(diff.Entries), "Diff should have 2 entry")
@@ -209,7 +209,7 @@ func TestSecretComparisonRemoveData(t *testing.T) {
209209
}
210210

211211
diff := CompareSecrets(a, b)
212-
diff.Print()
212+
diff.Print(false)
213213
assert.Equal(t, false, diff.Equal, "Secrets should not be equal")
214214
assert.Equal(t, SecretDiffTypeChanged, diff.Type, "Diff type should be changed")
215215
assert.Equal(t, 2, len(diff.Entries), "Diff should have 2 entry")
@@ -247,14 +247,18 @@ func TestSecretComparisonChangeData1(t *testing.T) {
247247
}
248248

249249
diff := CompareSecrets(a, b)
250-
diff.Print()
250+
diff.Print(false)
251251
assert.Equal(t, false, diff.Equal, "Secrets should not be equal")
252252
assert.Equal(t, SecretDiffTypeChanged, diff.Type, "Diff type should be changed")
253-
assert.Equal(t, 1, len(diff.Entries), "Diff should have 1 entry")
253+
assert.Equal(t, 2, len(diff.Entries), "Diff should have 2 entries")
254254

255255
entry1 := diff.GetEntry("data.key1")
256256
assert.NotNil(t, entry1, "Diff should have an entry for data.key1")
257257
assert.Equal(t, SecretDiffTypeChanged, entry1.Type, "DiffEntry type should be changed")
258+
259+
entry2 := diff.GetEntry("data.key2")
260+
assert.NotNil(t, entry2, "Diff should have an entry for data.key2")
261+
assert.Equal(t, SecretDiffTypeUnchanged, entry2.Type, "DiffEntry type should be unchanged")
258262
}
259263

260264
func TestSecretComparisonChangeData2(t *testing.T) {
@@ -281,7 +285,7 @@ func TestSecretComparisonChangeData2(t *testing.T) {
281285
}
282286

283287
diff := CompareSecrets(a, b)
284-
diff.Print()
288+
diff.Print(false)
285289
assert.Equal(t, false, diff.Equal, "Secrets should not be equal")
286290
assert.Equal(t, SecretDiffTypeChanged, diff.Type, "Diff type should be changed")
287291
assert.Equal(t, 2, len(diff.Entries), "Diff should have 2 entry")
@@ -319,7 +323,7 @@ func TestSecretComparisonChangeData3(t *testing.T) {
319323
}
320324

321325
diff := CompareSecrets(a, b)
322-
diff.Print()
326+
diff.Print(false)
323327
assert.Equal(t, false, diff.Equal, "Secrets should not be equal")
324328
assert.Equal(t, SecretDiffTypeChanged, diff.Type, "Diff type should be changed")
325329
assert.Equal(t, 3, len(diff.Entries), "Diff should have 3 entry")
@@ -350,7 +354,7 @@ func TestSecretComparisonRemoveSecret(t *testing.T) {
350354
}
351355

352356
diff := CompareSecrets(a, nil)
353-
diff.Print()
357+
diff.Print(false)
354358
assert.Equal(t, false, diff.Equal, "Secrets should not be equal")
355359
assert.Equal(t, SecretDiffTypeRemoved, diff.Type, "Diff type should be removed")
356360
assert.Equal(t, 2, len(diff.Entries), "Diff should have 2 entry")
@@ -377,7 +381,7 @@ func TestSecretComparisonAddSecret(t *testing.T) {
377381
}
378382

379383
diff := CompareSecrets(nil, a)
380-
diff.Print()
384+
diff.Print(false)
381385
assert.Equal(t, false, diff.Equal, "Secrets should not be equal")
382386
assert.Equal(t, SecretDiffTypeAdded, diff.Type, "Diff type should be added")
383387
assert.Equal(t, 2, len(diff.Entries), "Diff should have 2 entry")

0 commit comments

Comments
 (0)