Skip to content

Commit adb62fa

Browse files
CLOUDP-136708: AtlasCLI: Support Get/Update Audit configs for projects (#3036)
1 parent e442bcc commit adb62fa

File tree

12 files changed

+394
-8
lines changed

12 files changed

+394
-8
lines changed
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
.. _atlas-auditing-update:
2+
3+
=====================
4+
atlas auditing update
5+
=====================
6+
7+
.. default-domain:: mongodb
8+
9+
.. contents:: On this page
10+
:local:
11+
:backlinks: none
12+
:depth: 1
13+
:class: singlecol
14+
15+
Updates the auditing configuration for the specified project
16+
17+
To use this command, you must authenticate with a user account or an API key with the Project Owner role.
18+
19+
Syntax
20+
------
21+
22+
.. code-block::
23+
:caption: Command Syntax
24+
25+
atlas auditing update [options]
26+
27+
.. Code end marker, please don't delete this comment
28+
29+
Options
30+
-------
31+
32+
.. list-table::
33+
:header-rows: 1
34+
:widths: 20 10 10 60
35+
36+
* - Name
37+
- Type
38+
- Required
39+
- Description
40+
* - --auditAuthorizationSuccess
41+
-
42+
- false
43+
- Flag that indicates whether someone set auditing to track successful authentications. This only applies to the "atype" : "authCheck" audit filter. Setting this parameter to true degrades cluster performance.
44+
* - --auditFilter
45+
- string
46+
- false
47+
- JSON document that specifies which events to record. Escape any characters that may prevent parsing, such as single or double quotes, using a backslash (\).
48+
49+
Mutually exclusive with --file.
50+
* - --enabled
51+
-
52+
- false
53+
- Flag that indicates whether someone enabled database auditing for the specified project.
54+
* - -f, --file
55+
- string
56+
- false
57+
- Path to an optional JSON configuration file that defines auditing filters. To learn more about auditing configuration files for the Atlas CLI, see https://www.mongodb.com/docs/atlas/database-auditing/#example-auditing-filters
58+
59+
Mutually exclusive with --auditFilter.
60+
* - -h, --help
61+
-
62+
- false
63+
- help for update
64+
* - -o, --output
65+
- string
66+
- false
67+
- Output format. Valid values are json, json-path, go-template, or go-template-file. To see the full output, use the -o json option.
68+
* - --projectId
69+
- string
70+
- false
71+
- Hexadecimal string that identifies the project to use. This option overrides the settings in the configuration file or environment variable.
72+
73+
Inherited Options
74+
-----------------
75+
76+
.. list-table::
77+
:header-rows: 1
78+
:widths: 20 10 10 60
79+
80+
* - Name
81+
- Type
82+
- Required
83+
- Description
84+
* - -P, --profile
85+
- string
86+
- false
87+
- Name of the profile to use from your configuration file. To learn about profiles for the Atlas CLI, see https://dochub.mongodb.org/core/atlas-cli-save-connection-settings.
88+
89+
Output
90+
------
91+
92+
If the command succeeds, the CLI returns output similar to the following sample. Values in brackets represent your values.
93+
94+
.. code-block::
95+
96+
Auditing configuration successfully updated.
97+
98+
99+
Examples
100+
--------
101+
102+
.. code-block::
103+
:copyable: false
104+
105+
# Audit all authentication events for known users:
106+
atlas auditing update --auditFilter '{"atype": "authenticate"}'
107+
108+
109+
.. code-block::
110+
:copyable: false
111+
112+
# Audit all authentication events for known user via a configuration file:
113+
atlas auditing update -f filter.json
114+

docs/command/atlas-auditing.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,12 @@ Related Commands
5050
----------------
5151

5252
* :ref:`atlas-auditing-describe` - Returns the auditing configuration for the specified project.
53+
* :ref:`atlas-auditing-update` - Updates the auditing configuration for the specified project
5354

5455

5556
.. toctree::
5657
:titlesonly:
5758

5859
describe </command/atlas-auditing-describe>
60+
update </command/atlas-auditing-update>
5961

internal/cli/auditing/auditing.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ func Builder() *cobra.Command {
2525
}
2626
cmd.AddCommand(
2727
DescribeBuilder(),
28+
UpdateBuilder(),
2829
)
2930

3031
return cmd

internal/cli/auditing/auditing_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func TestBuilder(t *testing.T) {
2626
test.CmdValidator(
2727
t,
2828
Builder(),
29-
1,
29+
2,
3030
[]string{},
3131
)
3232
}

internal/cli/auditing/describe_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ import (
2222

2323
"github.com/golang/mock/gomock"
2424
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/cli"
25+
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/flag"
2526
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/mocks"
27+
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/test"
2628
"github.com/stretchr/testify/assert"
2729
"github.com/stretchr/testify/require"
2830
atlasv2 "go.mongodb.org/atlas-sdk/v20231115014/admin"
@@ -53,3 +55,12 @@ func TestDescribeOpts_Run(t *testing.T) {
5355
`, buf.String())
5456
t.Log(buf.String())
5557
}
58+
59+
func TestDescribeBuilder(t *testing.T) {
60+
test.CmdValidator(
61+
t,
62+
UpdateBuilder(),
63+
0,
64+
[]string{flag.Output, flag.ProjectID},
65+
)
66+
}

internal/cli/auditing/update.go

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
// Copyright 2024 MongoDB Inc
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package auditing
16+
17+
import (
18+
"context"
19+
"fmt"
20+
21+
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/cli"
22+
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/cli/require"
23+
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/config"
24+
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/file"
25+
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/flag"
26+
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/store"
27+
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/usage"
28+
"github.com/spf13/afero"
29+
"github.com/spf13/cobra"
30+
atlasv2 "go.mongodb.org/atlas-sdk/v20231115014/admin"
31+
)
32+
33+
type UpdateOpts struct {
34+
cli.GlobalOpts
35+
cli.OutputOpts
36+
enabled bool
37+
auditAuthorizationSuccess bool
38+
filename string
39+
auditFilter string
40+
fs afero.Fs
41+
store store.AuditingUpdater
42+
}
43+
44+
func (opts *UpdateOpts) initStore(ctx context.Context) func() error {
45+
return func() error {
46+
var err error
47+
opts.store, err = store.New(store.AuthenticatedPreset(config.Default()), store.WithContext(ctx))
48+
return err
49+
}
50+
}
51+
52+
var updateTemplate = "Auditing configuration successfully updated.\n"
53+
54+
func (opts *UpdateOpts) Run() error {
55+
auditLog, err := opts.newAuditLog()
56+
if err != nil {
57+
return err
58+
}
59+
r, err := opts.store.UpdateAuditingConfig(opts.ConfigProjectID(), auditLog)
60+
if err != nil {
61+
return err
62+
}
63+
return opts.Print(r)
64+
}
65+
66+
func (opts *UpdateOpts) newAuditLog() (*atlasv2.AuditLog, error) {
67+
if opts.filename != "" {
68+
fileContent, err := file.LoadFile(opts.fs, opts.filename)
69+
if err != nil {
70+
return nil, err
71+
}
72+
opts.auditFilter = string(fileContent)
73+
}
74+
75+
return &atlasv2.AuditLog{
76+
Enabled: &opts.enabled,
77+
AuditAuthorizationSuccess: &opts.auditAuthorizationSuccess,
78+
AuditFilter: &opts.auditFilter,
79+
}, nil
80+
}
81+
82+
// UpdateBuilder atlas auditing update [--file filter.json] [--auditFilter "{"atype": "authenticate"}"] [--auditAuthorizationSuccess] [--enabled] --projectId projectId.
83+
func UpdateBuilder() *cobra.Command {
84+
opts := &UpdateOpts{
85+
fs: afero.NewOsFs(),
86+
}
87+
cmd := &cobra.Command{
88+
Use: "update",
89+
Short: "Updates the auditing configuration for the specified project",
90+
Long: fmt.Sprintf(usage.RequiredRole, "Project Owner"),
91+
Args: require.NoArgs,
92+
Annotations: map[string]string{
93+
"output": updateTemplate,
94+
},
95+
Example: ` # Audit all authentication events for known users:
96+
atlas auditing update --auditFilter '{"atype": "authenticate"}'
97+
98+
# Audit all authentication events for known user via a configuration file:
99+
atlas auditing update -f filter.json
100+
`,
101+
PreRunE: func(cmd *cobra.Command, _ []string) error {
102+
return opts.PreRunE(
103+
opts.ValidateProjectID,
104+
opts.initStore(cmd.Context()),
105+
opts.InitOutput(cmd.OutOrStdout(), updateTemplate),
106+
)
107+
},
108+
RunE: func(_ *cobra.Command, _ []string) error {
109+
return opts.Run()
110+
},
111+
}
112+
113+
cmd.Flags().StringVar(&opts.ProjectID, flag.ProjectID, "", usage.ProjectID)
114+
cmd.Flags().StringVar(&opts.auditFilter, flag.AuditFilter, "", usage.AuditFilter)
115+
cmd.Flags().BoolVar(&opts.enabled, flag.Enabled, false, usage.EnabledAuditing)
116+
cmd.Flags().StringVarP(&opts.filename, flag.File, flag.FileShort, "", usage.AuditingFilename)
117+
cmd.Flags().BoolVar(&opts.auditAuthorizationSuccess, flag.AuditAuthorizationSuccess, false, usage.AuditAuthorizationSuccess)
118+
cmd.Flags().StringVarP(&opts.Output, flag.Output, flag.OutputShort, "", usage.FormatOut)
119+
_ = cmd.RegisterFlagCompletionFunc(flag.Output, opts.AutoCompleteOutputFlag())
120+
121+
_ = cmd.MarkFlagFilename(flag.File)
122+
123+
cmd.MarkFlagsMutuallyExclusive(flag.File, flag.AuditFilter)
124+
cmd.MarkFlagsOneRequired(flag.File, flag.AuditFilter)
125+
126+
return cmd
127+
}

internal/cli/auditing/update_test.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// Copyright 2024 MongoDB Inc
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
//go:build unit
16+
17+
package auditing
18+
19+
import (
20+
"bytes"
21+
"testing"
22+
23+
"github.com/golang/mock/gomock"
24+
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/cli"
25+
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/flag"
26+
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/mocks"
27+
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/pointer"
28+
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/test"
29+
"github.com/stretchr/testify/assert"
30+
"github.com/stretchr/testify/require"
31+
atlasv2 "go.mongodb.org/atlas-sdk/v20231115014/admin"
32+
)
33+
34+
func TestUpdateOpts_Run(t *testing.T) {
35+
ctrl := gomock.NewController(t)
36+
mockStore := mocks.NewMockAuditingUpdater(ctrl)
37+
buf := new(bytes.Buffer)
38+
opts := &UpdateOpts{
39+
store: mockStore,
40+
OutputOpts: cli.OutputOpts{
41+
OutWriter: buf,
42+
Template: updateTemplate,
43+
},
44+
auditFilter: "test",
45+
enabled: false,
46+
auditAuthorizationSuccess: false,
47+
}
48+
49+
expected := &atlasv2.AuditLog{
50+
Enabled: pointer.Get(false),
51+
AuditFilter: pointer.Get("test"),
52+
AuditAuthorizationSuccess: pointer.Get(false),
53+
}
54+
55+
body, err := opts.newAuditLog()
56+
require.NoError(t, err)
57+
mockStore.
58+
EXPECT().
59+
UpdateAuditingConfig(opts.ConfigProjectID(), body).
60+
Return(expected, nil).
61+
Times(1)
62+
63+
require.NoError(t, opts.Run())
64+
assert.Equal(t, "Auditing configuration successfully updated.\n", buf.String())
65+
t.Log(buf.String())
66+
}
67+
68+
func TestUpdateBuilder(t *testing.T) {
69+
test.CmdValidator(
70+
t,
71+
UpdateBuilder(),
72+
0,
73+
[]string{flag.Output, flag.ProjectID, flag.AuditAuthorizationSuccess, flag.AuditFilter, flag.Enabled, flag.File},
74+
)
75+
}

0 commit comments

Comments
 (0)