/
legalhold-set.go
221 lines (189 loc) · 6.6 KB
/
legalhold-set.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// Copyright (c) 2015-2022 MinIO, Inc.
//
// This file is part of MinIO Object Storage stack
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package cmd
import (
"context"
"fmt"
"path/filepath"
"strings"
"time"
"github.com/fatih/color"
"github.com/minio/cli"
"github.com/minio/minio-go/v7"
"github.com/minio/pkg/v2/console"
)
var lhSetFlags = []cli.Flag{
cli.BoolFlag{
Name: "recursive, r",
Usage: "apply legal hold recursively",
},
cli.StringFlag{
Name: "version-id, vid",
Usage: "apply legal hold to a specific object version",
},
cli.StringFlag{
Name: "rewind",
Usage: "apply legal hold on an object version at specified time",
},
cli.BoolFlag{
Name: "versions",
Usage: "apply legal hold on multiple versions of an object",
},
}
var legalHoldSetCmd = cli.Command{
Name: "set",
Usage: "set legal hold for object(s)",
Action: mainLegalHoldSet,
OnUsageError: onUsageError,
Before: setGlobalsFromContext,
Flags: append(lhSetFlags, globalFlags...),
CustomHelpTemplate: `NAME:
{{.HelpName}} - {{.Usage}}
USAGE:
{{.HelpName}} [FLAGS] TARGET
FLAGS:
{{range .VisibleFlags}}{{.}}
{{end}}
EXAMPLES:
1. Enable legal hold on a specific object
$ {{.HelpName}} myminio/mybucket/prefix/obj.csv
2. Enable legal hold on a specific object version
$ {{.HelpName}} myminio/mybucket/prefix/obj.csv --version-id "HiMFUTOowG6ylfNi4LKxD3ieHbgfgrvC"
3. Enable object legal hold recursively for all objects at a prefix
$ {{.HelpName}} myminio/mybucket/prefix --recursive
4. Enable object legal hold recursively for all objects versions older than one year
$ {{.HelpName}} myminio/mybucket/prefix --recursive --rewind 365d --versions
`,
}
// setLegalHold - Set legalhold for all objects within a given prefix.
func setLegalHold(ctx context.Context, urlStr, versionID string, timeRef time.Time, withOlderVersions, recursive bool, lhold minio.LegalHoldStatus) error {
clnt, err := newClient(urlStr)
if err != nil {
fatalIf(err.Trace(), "Unable to parse the provided url.")
}
prefixPath := clnt.GetURL().Path
prefixPath = filepath.ToSlash(prefixPath)
if !strings.HasSuffix(prefixPath, "/") {
prefixPath = prefixPath[:strings.LastIndex(prefixPath, "/")+1]
}
prefixPath = strings.TrimPrefix(prefixPath, "./")
if !recursive && !withOlderVersions {
err = clnt.PutObjectLegalHold(ctx, versionID, lhold)
if err != nil {
errorIf(err.Trace(urlStr), "Failed to set legal hold on `"+urlStr+"` successfully")
} else {
contentURL := filepath.ToSlash(clnt.GetURL().Path)
key := strings.TrimPrefix(contentURL, prefixPath)
printMsg(legalHoldCmdMessage{
LegalHold: lhold,
Status: "success",
URLPath: clnt.GetURL().String(),
Key: key,
VersionID: versionID,
})
}
return nil
}
alias, _, _ := mustExpandAlias(urlStr)
var cErr error
objectsFound := false
lstOptions := ListOptions{Recursive: recursive, ShowDir: DirNone}
if !timeRef.IsZero() {
lstOptions.WithOlderVersions = withOlderVersions
lstOptions.TimeRef = timeRef
}
for content := range clnt.List(ctx, lstOptions) {
if content.Err != nil {
errorIf(content.Err.Trace(clnt.GetURL().String()), "Unable to list folder.")
cErr = exitStatus(globalErrorExitStatus) // Set the exit status.
continue
}
if !recursive && alias+getKey(content) != getStandardizedURL(urlStr) {
break
}
objectsFound = true
newClnt, perr := newClientFromAlias(alias, content.URL.String())
if perr != nil {
errorIf(content.Err.Trace(clnt.GetURL().String()), "Invalid URL")
continue
}
probeErr := newClnt.PutObjectLegalHold(ctx, content.VersionID, lhold)
if probeErr != nil {
errorIf(probeErr.Trace(content.URL.Path), "Failed to set legal hold on `"+content.URL.Path+"` successfully")
} else {
if !globalJSON {
contentURL := filepath.ToSlash(content.URL.Path)
key := strings.TrimPrefix(contentURL, prefixPath)
printMsg(legalHoldCmdMessage{
LegalHold: lhold,
Status: "success",
URLPath: content.URL.String(),
Key: key,
VersionID: content.VersionID,
})
}
}
}
if cErr == nil && !globalJSON {
if !objectsFound {
console.Print(console.Colorize("LegalHoldMessageFailure",
fmt.Sprintf("No objects/versions found while setting legal hold on `%s`. \n", urlStr)))
}
}
return cErr
}
// Validate command line arguments.
func parseLegalHoldArgs(cliCtx *cli.Context) (targetURL, versionID string, timeRef time.Time, recursive, withVersions bool) {
args := cliCtx.Args()
if len(args) != 1 {
showCommandHelpAndExit(cliCtx, 1)
}
targetURL = args[0]
if targetURL == "" {
fatalIf(errInvalidArgument(), "You cannot pass an empty target url.")
}
versionID = cliCtx.String("version-id")
recursive = cliCtx.Bool("recursive")
withVersions = cliCtx.Bool("versions")
rewind := cliCtx.String("rewind")
if versionID != "" && (recursive || withVersions || rewind != "") {
fatalIf(errInvalidArgument(), "You cannot pass --version-id with any of --versions, --recursive and --rewind flags.")
}
timeRef = parseRewindFlag(rewind)
return
}
// main for legalhold set command.
func mainLegalHoldSet(cliCtx *cli.Context) error {
console.SetColor("LegalHoldSuccess", color.New(color.FgGreen, color.Bold))
console.SetColor("LegalHoldFailure", color.New(color.FgRed, color.Bold))
console.SetColor("LegalHoldPartialFailure", color.New(color.FgRed, color.Bold))
console.SetColor("LegalHoldMessageFailure", color.New(color.FgYellow))
targetURL, versionID, timeRef, recursive, withVersions := parseLegalHoldArgs(cliCtx)
if timeRef.IsZero() && withVersions {
timeRef = time.Now().UTC()
}
ctx, cancelLegalHold := context.WithCancel(globalContext)
defer cancelLegalHold()
enabled, err := isBucketLockEnabled(ctx, targetURL)
if err != nil {
fatalIf(err, "Unable to set legalhold on `%s`", targetURL)
}
if !enabled {
fatalIf(errDummy().Trace(), "Bucket lock needs to be enabled in order to use this feature.")
}
return setLegalHold(ctx, targetURL, versionID, timeRef, withVersions, recursive, minio.LegalHoldEnabled)
}