Skip to content

Commit 123e53c

Browse files
author
Jan Kaluza
committed
Add "create" and "remove" events for secrets.
This commit adds the "secret" Event type and emits "create" and "remove" events for this Event type when Secret is created or removed. This can be used for example by podman interfaces to view and manage secrets. Fixes: containers#24030 Signed-off-by: Jan Kaluza <[email protected]>
1 parent 350429c commit 123e53c

File tree

8 files changed

+40
-2
lines changed

8 files changed

+40
-2
lines changed

cmd/podman/common/completion.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1522,7 +1522,7 @@ func AutocompleteEventFilter(cmd *cobra.Command, args []string, toComplete strin
15221522
}
15231523
eventTypes := func(_ string) ([]string, cobra.ShellCompDirective) {
15241524
return []string{events.Container.String(), events.Image.String(), events.Network.String(),
1525-
events.Pod.String(), events.System.String(), events.Volume.String(),
1525+
events.Pod.String(), events.System.String(), events.Volume.String(), events.Secret.String(),
15261526
}, cobra.ShellCompDirectiveNoFileComp
15271527
}
15281528
kv := keyValueCompletion{

docs/source/markdown/podman-events.1.md

+4
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ The *volume* type reports the following statuses:
7979
* prune
8080
* remove
8181

82+
The *secret* type reports the following statuses:
83+
* create
84+
* remove
85+
8286
#### Verbose Create Events
8387

8488
Setting `events_container_create_inspect_data=true` in containers.conf(5) instructs Podman to create more verbose container-create events which include a JSON payload with detailed information about the containers. The JSON payload is identical to the one of podman-container-inspect(1). The associated field in journald is named `PODMAN_CONTAINER_INSPECT_DATA`.

libpod/events.go

+10
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,16 @@ func (v *Volume) newVolumeEvent(status events.Status) {
191191
}
192192
}
193193

194+
// newSecretEvent creates a new event for a libpod secret
195+
func (r *Runtime) NewSecretEvent(status events.Status, secretID string) {
196+
e := events.NewEvent(status)
197+
e.ID = secretID
198+
e.Type = events.Secret
199+
if err := r.eventer.Write(e); err != nil {
200+
logrus.Errorf("Unable to write secret event: %q", err)
201+
}
202+
}
203+
194204
// Events is a wrapper function for everyone to begin tailing the events log
195205
// with options
196206
func (r *Runtime) Events(ctx context.Context, options events.ReadOptions) error {

libpod/events/config.go

+2
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ const (
131131
Volume Type = "volume"
132132
// Machine - event is related to machine VM's
133133
Machine Type = "machine"
134+
// Secret - event is related to secrets
135+
Secret Type = "secret"
134136

135137
// Attach ...
136138
Attach Status = "attach"

libpod/events/events.go

+4
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ func (e *Event) ToHumanReadable(truncate bool) string {
9292
}
9393
case Volume, Machine:
9494
humanFormat = fmt.Sprintf("%s %s %s %s", e.Time, e.Type, e.Status, e.Name)
95+
case Secret:
96+
humanFormat = fmt.Sprintf("%s %s %s %s", e.Time, e.Type, e.Status, id)
9597
}
9698
return humanFormat
9799
}
@@ -133,6 +135,8 @@ func StringToType(name string) (Type, error) {
133135
return System, nil
134136
case Volume.String():
135137
return Volume, nil
138+
case Secret.String():
139+
return Secret, nil
136140
case "":
137141
return "", ErrEventTypeBlank
138142
}

libpod/events/logfile.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ func (e EventLogFile) Read(ctx context.Context, options ReadOptions) error {
173173
continue
174174
}
175175
switch event.Type {
176-
case Image, Volume, Pod, Container, Network:
176+
case Image, Volume, Pod, Container, Network, Secret:
177177
// no-op
178178
case System:
179179
begin, end, err := e.readRotateEvent(event)

pkg/domain/infra/abi/secrets.go

+4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"strings"
1212

1313
"github.com/containers/common/pkg/secrets"
14+
"github.com/containers/podman/v5/libpod/events"
1415
"github.com/containers/podman/v5/pkg/domain/entities"
1516
"github.com/containers/podman/v5/pkg/domain/utils"
1617
)
@@ -56,6 +57,8 @@ func (ic *ContainerEngine) SecretCreate(ctx context.Context, name string, reader
5657
return nil, err
5758
}
5859

60+
ic.Libpod.NewSecretEvent(events.Create, secretID)
61+
5962
return &entities.SecretCreateReport{
6063
ID: secretID,
6164
}, nil
@@ -146,6 +149,7 @@ func (ic *ContainerEngine) SecretRm(ctx context.Context, nameOrIDs []string, opt
146149
continue
147150
}
148151
reports = append(reports, &entities.SecretRmReport{Err: err, ID: deletedID})
152+
ic.Libpod.NewSecretEvent(events.Remove, deletedID)
149153
}
150154

151155
return reports, nil

test/e2e/secret_test.go

+14
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ var _ = Describe("Podman secret", func() {
2929
secrID := session.OutputToString()
3030
Expect(session).Should(ExitCleanly())
3131

32+
result := podmanTest.Podman([]string{"events", "--stream=false"})
33+
result.WaitWithDefaultTimeout()
34+
Expect(result).Should(ExitCleanly())
35+
events := result.OutputToStringArray()
36+
Expect(events).ToNot(BeEmpty(), "Number of events")
37+
Expect(events).To(ContainElement(ContainSubstring(fmt.Sprintf(" secret create %s", secrID))))
38+
3239
inspect := podmanTest.Podman([]string{"secret", "inspect", "--format", "{{.ID}}", secrID})
3340
inspect.WaitWithDefaultTimeout()
3441
Expect(inspect).Should(ExitCleanly())
@@ -305,6 +312,13 @@ var _ = Describe("Podman secret", func() {
305312
Expect(removed).Should(ExitCleanly())
306313
Expect(removed.OutputToString()).To(Equal(secrID))
307314

315+
result := podmanTest.Podman([]string{"events", "--stream=false"})
316+
result.WaitWithDefaultTimeout()
317+
Expect(result).Should(ExitCleanly())
318+
events := result.OutputToStringArray()
319+
Expect(events).ToNot(BeEmpty(), "Number of events")
320+
Expect(events).To(ContainElement(ContainSubstring(fmt.Sprintf(" secret remove %s", secrID))))
321+
308322
session = podmanTest.Podman([]string{"secret", "ls"})
309323
session.WaitWithDefaultTimeout()
310324
Expect(session).Should(ExitCleanly())

0 commit comments

Comments
 (0)