|
4 | 4 | package consul |
5 | 5 |
|
6 | 6 | import ( |
| 7 | + "bytes" |
7 | 8 | "context" |
8 | 9 | "fmt" |
9 | 10 |
|
@@ -86,6 +87,11 @@ func (r *aclTokenReplicator) FetchUpdated(srv *Server, updates []string) (int, e |
86 | 87 | return len(r.updated), nil |
87 | 88 | } |
88 | 89 |
|
| 90 | +func (r *aclTokenReplicator) ensureRemoteConsistent(updates []string) ([]string, []string, error) { |
| 91 | + //return true if consistent updates, |
| 92 | + return []string{}, []string{}, nil |
| 93 | +} |
| 94 | + |
89 | 95 | func (r *aclTokenReplicator) DeleteLocalBatch(srv *Server, batch []string) error { |
90 | 96 | req := structs.ACLTokenBatchDeleteRequest{ |
91 | 97 | TokenIDs: batch, |
@@ -186,6 +192,45 @@ func (r *aclPolicyReplicator) FetchUpdated(srv *Server, updates []string) (int, |
186 | 192 | return len(r.updated), nil |
187 | 193 | } |
188 | 194 |
|
| 195 | +func (r *aclPolicyReplicator) ensureRemoteConsistent(updates []string) ([]string, []string, error) { |
| 196 | + updatedMap := make(map[string]*structs.ACLPolicy) |
| 197 | + for _, policy := range r.updated { |
| 198 | + updatedMap[policy.ID] = policy |
| 199 | + } |
| 200 | + |
| 201 | + remoteMap := make(map[string]*structs.ACLPolicyListStub) |
| 202 | + for _, policyStub := range r.remote { |
| 203 | + remoteMap[policyStub.ID] = policyStub |
| 204 | + } |
| 205 | + |
| 206 | + //iterate over all updates array which are policy IDs check if the hash match for both |
| 207 | + var consistent = true |
| 208 | + var remoteNotCreated []string |
| 209 | + var remoteNotUpdated []string |
| 210 | + var err error = nil |
| 211 | + |
| 212 | + for _, policyID := range updates { |
| 213 | + if updatedPolicy, ok := updatedMap[policyID]; ok { |
| 214 | + if remotePolicy, ok := remoteMap[policyID]; ok { |
| 215 | + if !bytes.Equal(updatedPolicy.Hash, remotePolicy.Hash) && updatedPolicy.ModifyIndex < remotePolicy.ModifyIndex { |
| 216 | + // remote stale batch did not get modified policy than what local diff calculated |
| 217 | + remoteNotUpdated = append(remoteNotUpdated, policyID) |
| 218 | + consistent = false |
| 219 | + } |
| 220 | + } |
| 221 | + } else if remotePolicy, ok := remoteMap[policyID]; ok && remotePolicy.ModifyIndex == remotePolicy.CreateIndex { |
| 222 | + // remote stale batch did not get the created policy than what local diff calculated |
| 223 | + remoteNotCreated = append(remoteNotCreated, policyID) |
| 224 | + consistent = false |
| 225 | + } |
| 226 | + } |
| 227 | + |
| 228 | + if !consistent { |
| 229 | + err = errContainsStaleData |
| 230 | + } |
| 231 | + return remoteNotCreated, remoteNotUpdated, err |
| 232 | +} |
| 233 | + |
189 | 234 | func (r *aclPolicyReplicator) DeleteLocalBatch(srv *Server, batch []string) error { |
190 | 235 | req := structs.ACLPolicyBatchDeleteRequest{ |
191 | 236 | PolicyIDs: batch, |
@@ -307,6 +352,11 @@ func (r *aclRoleReplicator) FetchUpdated(srv *Server, updates []string) (int, er |
307 | 352 | return len(r.updated), nil |
308 | 353 | } |
309 | 354 |
|
| 355 | +func (r *aclRoleReplicator) ensureRemoteConsistent(updates []string) ([]string, []string, error) { |
| 356 | + //return true if consistent updates, |
| 357 | + return []string{}, []string{}, nil |
| 358 | +} |
| 359 | + |
310 | 360 | func (r *aclRoleReplicator) DeleteLocalBatch(srv *Server, batch []string) error { |
311 | 361 | req := structs.ACLRoleBatchDeleteRequest{ |
312 | 362 | RoleIDs: batch, |
|
0 commit comments