This repository has been archived by the owner on Jul 26, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
/
libentitlement.go
198 lines (158 loc) · 4.82 KB
/
libentitlement.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
package libentitlement
import (
"fmt"
"strings"
"github.com/sirupsen/logrus"
"github.com/moby/libentitlement/defaults"
"github.com/moby/libentitlement/domain"
"github.com/moby/libentitlement/entitlement"
secprofile "github.com/moby/libentitlement/secprofile"
)
// EntitlementsManager generates enforceable profiles from its entitlements and domains state
type EntitlementsManager struct {
profile secprofile.Profile
entitlementList []entitlement.Entitlement
domainManager *domainmanager.DomainManager
}
// NewEntitlementsManager instantiates an EntitlementsManager object with the given profile
// default
func NewEntitlementsManager(profile secprofile.Profile) *EntitlementsManager {
if profile == nil {
logrus.Errorf("Entilements Manager initialization: invalid security profile - cannot be nil")
return nil
}
return &EntitlementsManager{
profile: profile,
entitlementList: make([]entitlement.Entitlement, 0),
domainManager: domainmanager.NewDomainManager(),
}
}
// GetProfile returns the current state of the security profile
func (m *EntitlementsManager) GetProfile() (secprofile.Profile, error) {
if m.profile == nil {
return nil, fmt.Errorf("Entitlements Manager doesn't have a security profile")
}
return m.profile, nil
}
// SetProfile sets the entitlement manager's security profile
func (m *EntitlementsManager) SetProfile(profile secprofile.Profile) error {
if profile == nil {
return fmt.Errorf("Invalid security profile")
}
m.profile = profile
return nil
}
func isValidEntitlement(ent entitlement.Entitlement) (bool, error) {
_, err := ent.Identifier()
if err != nil {
return false, err
}
_, err = ent.Domain()
if err != nil {
return false, err
}
_, err = ent.Value()
if err != nil {
return false, err
}
return true, nil
}
// AddDefault adds a default entitlement identified by entName which must be a default identifier.
func (m *EntitlementsManager) AddDefault(entName string) error {
defaultEnt, ok := defaults.DefaultEntitlements[entName]
if !ok {
return fmt.Errorf("Couldn't add invalid default entitlement name: %s", entName)
}
return m.Add(defaultEnt)
}
// Add adds the given entitlements to the current entitlements list, updates the domain name system and enforce
// the entitlement on the security profile
func (m *EntitlementsManager) Add(entitlements ...entitlement.Entitlement) error {
if m.profile == nil {
return fmt.Errorf("Couldn't add to invalid security profile")
}
for _, ent := range entitlements {
if isValid, err := isValidEntitlement(ent); isValid == false {
return fmt.Errorf("Couldn't add invalid entitlement: %v", err)
}
profile, err := ent.Enforce(m.profile)
if err != nil {
return err
}
identifier, _ := ent.Identifier()
domainString, _ := ent.Domain()
domainList := strings.Split(domainString, ".")
err = m.domainManager.AddFullDomainWithEntitlementID(domainList, identifier)
if err != nil {
// Should not happen since we verified isValidEntitlement
// FIXME: we should probably revert the changes on the security profile
return err
}
m.profile = profile
m.entitlementList = append(m.entitlementList, ent)
}
return nil
}
func isEqual(ent1, ent2 entitlement.Entitlement) (bool, error) {
id1, err := ent1.Identifier()
if err != nil {
return false, err
}
dom1, err := ent1.Domain()
if err != nil {
return false, err
}
val1, err := ent1.Value()
if err != nil {
return false, err
}
id2, err := ent2.Identifier()
if err != nil {
return false, err
}
dom2, err := ent2.Domain()
if err != nil {
return false, err
}
val2, err := ent2.Value()
if err != nil {
return false, err
}
return id1 == id2 && dom1 == dom2 && val1 == val2, nil
}
// HasEntitlement returns whether the given entitlement is registered in the current entitlements list
func (m *EntitlementsManager) HasEntitlement(ent entitlement.Entitlement) (bool, error) {
if isValid, err := isValidEntitlement(ent); isValid == false {
return false, fmt.Errorf("Couldn't check invalid entitlement: %v", err)
}
for _, currEnt := range m.entitlementList {
// Only compare entitlements id, domain and value
entEqual, err := isEqual(currEnt, ent)
if err != nil {
return false, err
}
if entEqual == true {
return true, nil
}
}
return false, nil
}
// Enforce applies the constraints on the security profile and updates it to be used for the container
func (m *EntitlementsManager) Enforce() error {
for _, ent := range m.entitlementList {
if isValid, err := isValidEntitlement(ent); isValid == false {
return fmt.Errorf("Couldn't enforce invalid entitlement: %v", err)
}
// Try to enforce the entitlement on the security profile
profile, err := m.GetProfile()
if err != nil {
return err
}
newProfile, err := ent.Enforce(profile)
if err != nil {
return err
}
m.profile = newProfile
}
return nil
}