-
Notifications
You must be signed in to change notification settings - Fork 787
/
attrValidator.go
163 lines (139 loc) · 5.05 KB
/
attrValidator.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
// Copyright (c) 2017 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package domain
import (
"fmt"
"github.com/uber/cadence/common/cluster"
"github.com/uber/cadence/common/persistence"
"github.com/uber/cadence/common/types"
)
type (
// AttrValidatorImpl is domain attr validator
AttrValidatorImpl struct {
clusterMetadata cluster.Metadata
minRetentionDays int32
}
)
// newAttrValidator create a new domain attr validator
func newAttrValidator(
clusterMetadata cluster.Metadata,
minRetentionDays int32,
) *AttrValidatorImpl {
return &AttrValidatorImpl{
clusterMetadata: clusterMetadata,
minRetentionDays: minRetentionDays,
}
}
func (d *AttrValidatorImpl) validateDomainConfig(config *persistence.DomainConfig) error {
if config.Retention < int32(d.minRetentionDays) {
return errInvalidRetentionPeriod
}
if config.HistoryArchivalStatus == types.ArchivalStatusEnabled && len(config.HistoryArchivalURI) == 0 {
return errInvalidArchivalConfig
}
if config.VisibilityArchivalStatus == types.ArchivalStatusEnabled && len(config.VisibilityArchivalURI) == 0 {
return errInvalidArchivalConfig
}
return nil
}
func (d *AttrValidatorImpl) validateDomainReplicationConfigForLocalDomain(
replicationConfig *persistence.DomainReplicationConfig,
) error {
activeCluster := replicationConfig.ActiveClusterName
clusters := replicationConfig.Clusters
if err := d.validateClusterName(activeCluster); err != nil {
return err
}
for _, clusterConfig := range clusters {
if err := d.validateClusterName(clusterConfig.ClusterName); err != nil {
return err
}
}
if activeCluster != d.clusterMetadata.GetCurrentClusterName() {
return &types.BadRequestError{Message: "Invalid local domain active cluster"}
}
if len(clusters) != 1 || clusters[0].ClusterName != activeCluster {
return &types.BadRequestError{Message: "Invalid local domain clusters"}
}
return nil
}
func (d *AttrValidatorImpl) validateDomainReplicationConfigForGlobalDomain(
replicationConfig *persistence.DomainReplicationConfig,
) error {
// TODO: https://github.com/uber/cadence/issues/4345 add checking for "pending active" as well
// Right now we only have checking if clusters to remove are "current active cluster" in this method.
// However, there could be edge cases that a cluster is in "pending active" state during graceful failover.
// It's better to do this check so that people won't make mistake.
// However, this is not critical -- even this happens, they can add the active cluster back
activeCluster := replicationConfig.ActiveClusterName
clusters := replicationConfig.Clusters
if err := d.validateClusterName(activeCluster); err != nil {
return err
}
for _, clusterConfig := range clusters {
if err := d.validateClusterName(clusterConfig.ClusterName); err != nil {
return err
}
}
activeClusterInClusters := false
for _, clusterConfig := range clusters {
if clusterConfig.ClusterName == activeCluster {
activeClusterInClusters = true
break
}
}
if !activeClusterInClusters {
return errActiveClusterNotInClusters
}
return nil
}
func (d *AttrValidatorImpl) validateDomainReplicationConfigClustersDoesNotRemove(
clustersOld []*persistence.ClusterReplicationConfig,
clustersNew []*persistence.ClusterReplicationConfig,
) error {
clusterNamesOld := make(map[string]bool)
for _, clusterConfig := range clustersOld {
clusterNamesOld[clusterConfig.ClusterName] = true
}
clusterNamesNew := make(map[string]bool)
for _, clusterConfig := range clustersNew {
clusterNamesNew[clusterConfig.ClusterName] = true
}
if len(clusterNamesNew) < len(clusterNamesOld) {
return errCannotRemoveClustersFromDomain
}
for clusterName := range clusterNamesOld {
if _, ok := clusterNamesNew[clusterName]; !ok {
return errCannotRemoveClustersFromDomain
}
}
return nil
}
func (d *AttrValidatorImpl) validateClusterName(
clusterName string,
) error {
if _, ok := d.clusterMetadata.GetEnabledClusterInfo()[clusterName]; !ok {
return &types.BadRequestError{Message: fmt.Sprintf(
"Invalid cluster name: %v",
clusterName,
)}
}
return nil
}