go.temporal.io/server@v1.23.0/common/namespace/attr_validator.go (about)

     1  // The MIT License
     2  //
     3  // Copyright (c) 2020 Temporal Technologies Inc.  All rights reserved.
     4  //
     5  // Copyright (c) 2020 Uber Technologies, Inc.
     6  //
     7  // Permission is hereby granted, free of charge, to any person obtaining a copy
     8  // of this software and associated documentation files (the "Software"), to deal
     9  // in the Software without restriction, including without limitation the rights
    10  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    11  // copies of the Software, and to permit persons to whom the Software is
    12  // furnished to do so, subject to the following conditions:
    13  //
    14  // The above copyright notice and this permission notice shall be included in
    15  // all copies or substantial portions of the Software.
    16  //
    17  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    18  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    19  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    20  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    21  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    22  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    23  // THE SOFTWARE.
    24  
    25  package namespace
    26  
    27  import (
    28  	"fmt"
    29  
    30  	enumspb "go.temporal.io/api/enums/v1"
    31  	"go.temporal.io/api/serviceerror"
    32  
    33  	persistencespb "go.temporal.io/server/api/persistence/v1"
    34  	"go.temporal.io/server/common/cluster"
    35  )
    36  
    37  type (
    38  	// AttrValidatorImpl is namespace attr validator
    39  	AttrValidatorImpl struct {
    40  		clusterMetadata cluster.Metadata
    41  	}
    42  )
    43  
    44  // NewAttrValidator create a new namespace attr validator
    45  func NewAttrValidator(
    46  	clusterMetadata cluster.Metadata,
    47  ) *AttrValidatorImpl {
    48  
    49  	return &AttrValidatorImpl{
    50  		clusterMetadata: clusterMetadata,
    51  	}
    52  }
    53  
    54  func (d *AttrValidatorImpl) ValidateNamespaceConfig(config *persistencespb.NamespaceConfig) error {
    55  	if config.HistoryArchivalState == enumspb.ARCHIVAL_STATE_ENABLED && len(config.HistoryArchivalUri) == 0 {
    56  		return errInvalidArchivalConfig
    57  	}
    58  	if config.VisibilityArchivalState == enumspb.ARCHIVAL_STATE_ENABLED && len(config.VisibilityArchivalUri) == 0 {
    59  		return errInvalidArchivalConfig
    60  	}
    61  	return nil
    62  }
    63  
    64  func (d *AttrValidatorImpl) ValidateNamespaceReplicationConfigForLocalNamespace(
    65  	replicationConfig *persistencespb.NamespaceReplicationConfig,
    66  ) error {
    67  
    68  	activeCluster := replicationConfig.ActiveClusterName
    69  	clusters := replicationConfig.Clusters
    70  
    71  	if err := d.validateClusterName(activeCluster); err != nil {
    72  		return err
    73  	}
    74  	for _, clusterName := range clusters {
    75  		if err := d.validateClusterName(clusterName); err != nil {
    76  			return err
    77  		}
    78  	}
    79  
    80  	if activeCluster != d.clusterMetadata.GetCurrentClusterName() {
    81  		return serviceerror.NewInvalidArgument("Invalid local namespace active cluster")
    82  	}
    83  
    84  	if len(clusters) != 1 || clusters[0] != activeCluster {
    85  		return serviceerror.NewInvalidArgument("Invalid local namespace clusters")
    86  	}
    87  
    88  	return nil
    89  }
    90  
    91  func (d *AttrValidatorImpl) ValidateNamespaceReplicationConfigForGlobalNamespace(
    92  	replicationConfig *persistencespb.NamespaceReplicationConfig,
    93  ) error {
    94  
    95  	activeCluster := replicationConfig.ActiveClusterName
    96  	clusters := replicationConfig.Clusters
    97  
    98  	if err := d.validateClusterName(activeCluster); err != nil {
    99  		return err
   100  	}
   101  	for _, clusterName := range clusters {
   102  		if err := d.validateClusterName(clusterName); err != nil {
   103  			return err
   104  		}
   105  	}
   106  
   107  	activeClusterInClusters := false
   108  	for _, clusterName := range clusters {
   109  		if clusterName == activeCluster {
   110  			activeClusterInClusters = true
   111  			break
   112  		}
   113  	}
   114  	if !activeClusterInClusters {
   115  		return errActiveClusterNotInClusters
   116  	}
   117  
   118  	return nil
   119  }
   120  
   121  func (d *AttrValidatorImpl) validateClusterName(
   122  	clusterName string,
   123  ) error {
   124  
   125  	if info, ok := d.clusterMetadata.GetAllClusterInfo()[clusterName]; !ok || !info.Enabled {
   126  		return serviceerror.NewInvalidArgument(fmt.Sprintf("Invalid cluster name: %v", clusterName))
   127  	}
   128  	return nil
   129  }