-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
lock.go
231 lines (191 loc) · 5.47 KB
/
lock.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
222
223
224
225
226
227
228
229
230
231
/*
Copyright 2021 Gravitational, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package types
import (
"strings"
"time"
"github.com/gravitational/teleport/api/v7/utils"
"github.com/gogo/protobuf/proto"
"github.com/google/go-cmp/cmp"
"github.com/gravitational/trace"
)
// Lock configures locking out of a particular access vector.
type Lock interface {
Resource
// Target returns the lock's target.
Target() LockTarget
// SetTarget sets the lock's target.
SetTarget(LockTarget)
// Message returns the message displayed to locked-out users.
Message() string
// SetMessage sets the lock's user message.
SetMessage(string)
// LockExpiry returns when the lock ceases to be in force.
LockExpiry() *time.Time
// SetLockExpiry sets the lock's expiry.
SetLockExpiry(*time.Time)
// IsInForce returns whether the lock is in force at a particular time.
IsInForce(time.Time) bool
}
// NewLock is a convenience method to create a Lock resource.
func NewLock(name string, spec LockSpecV2) (Lock, error) {
lock := &LockV2{
Metadata: Metadata{
Name: name,
},
Spec: spec,
}
if err := lock.CheckAndSetDefaults(); err != nil {
return nil, trace.Wrap(err)
}
return lock, nil
}
// GetVersion returns resource version.
func (c *LockV2) GetVersion() string {
return c.Version
}
// GetName returns the name of the resource.
func (c *LockV2) GetName() string {
return c.Metadata.Name
}
// SetName sets the name of the resource.
func (c *LockV2) SetName(e string) {
c.Metadata.Name = e
}
// SetExpiry sets expiry time for the object.
func (c *LockV2) SetExpiry(expires time.Time) {
c.Metadata.SetExpiry(expires)
}
// Expiry returns object expiry setting.
func (c *LockV2) Expiry() time.Time {
return c.Metadata.Expiry()
}
// GetMetadata returns object metadata.
func (c *LockV2) GetMetadata() Metadata {
return c.Metadata
}
// GetResourceID returns resource ID.
func (c *LockV2) GetResourceID() int64 {
return c.Metadata.ID
}
// SetResourceID sets resource ID.
func (c *LockV2) SetResourceID(id int64) {
c.Metadata.ID = id
}
// GetKind returns resource kind.
func (c *LockV2) GetKind() string {
return c.Kind
}
// GetSubKind returns resource subkind.
func (c *LockV2) GetSubKind() string {
return c.SubKind
}
// SetSubKind sets resource subkind.
func (c *LockV2) SetSubKind(sk string) {
c.SubKind = sk
}
// Target returns the lock's target.
func (c *LockV2) Target() LockTarget {
return c.Spec.Target
}
// SetTarget sets the lock's target.
func (c *LockV2) SetTarget(target LockTarget) {
c.Spec.Target = target
}
// Message returns the message displayed to locked-out users.
func (c *LockV2) Message() string {
return c.Spec.Message
}
// SetMessage sets the lock's user message.
func (c *LockV2) SetMessage(message string) {
c.Spec.Message = message
}
// LockExpiry returns when the lock ceases to be in force.
func (c *LockV2) LockExpiry() *time.Time {
return c.Spec.Expires
}
// SetLockExpiry sets the lock's expiry.
func (c *LockV2) SetLockExpiry(expiry *time.Time) {
c.Spec.Expires = expiry
}
// IsInForce returns whether the lock is in force at a particular time.
func (c *LockV2) IsInForce(t time.Time) bool {
if c.Spec.Expires == nil {
return true
}
return t.Before(*c.Spec.Expires)
}
// setStaticFields sets static resource header and metadata fields.
func (c *LockV2) setStaticFields() {
c.Kind = KindLock
c.Version = V2
}
// CheckAndSetDefaults verifies the constraints for Lock.
func (c *LockV2) CheckAndSetDefaults() error {
c.setStaticFields()
err := c.Metadata.CheckAndSetDefaults()
if err != nil {
return trace.Wrap(err)
}
if c.Spec.Target.IsEmpty() {
return trace.BadParameter("at least one target field must be set")
}
return nil
}
// IsEmpty returns true if none of the target's fields is set.
func (t LockTarget) IsEmpty() bool {
return cmp.Equal(t, LockTarget{})
}
// IntoMap returns the target attributes in the form of a map.
func (t LockTarget) IntoMap() (map[string]string, error) {
m := map[string]string{}
if err := utils.ObjectToStruct(t, &m); err != nil {
return nil, trace.Wrap(err)
}
return m, nil
}
// FromMap copies values from a map into this LockTarget.
func (t *LockTarget) FromMap(m map[string]string) error {
return trace.Wrap(utils.ObjectToStruct(m, t))
}
// Match returns true if the lock's target is matched by this target.
func (t LockTarget) Match(lock Lock) bool {
if t.IsEmpty() {
return false
}
lockTarget := lock.Target()
if t.User != "" && lockTarget.User != t.User {
return false
}
if t.Role != "" && lockTarget.Role != t.Role {
return false
}
if t.Login != "" && lockTarget.Login != t.Login {
return false
}
if t.Node != "" && lockTarget.Node != t.Node {
return false
}
if t.MFADevice != "" && lockTarget.MFADevice != t.MFADevice {
return false
}
return true
}
// String returns string representation of the LockTarget.
func (t LockTarget) String() string {
return strings.TrimSpace(proto.CompactTextString(&t))
}
// Equals returns true when the two lock targets are equal.
func (t LockTarget) Equals(t2 LockTarget) bool {
return proto.Equal(&t, &t2)
}