github.com/choria-io/go-choria@v0.28.1-0.20240416190746-b3bf9c7d5a45/filter/identity/identity.go (about)

     1  // Copyright (c) 2019-2021, R.I. Pienaar and the Choria Project contributors
     2  //
     3  // SPDX-License-Identifier: Apache-2.0
     4  
     5  package identity
     6  
     7  import (
     8  	"regexp"
     9  	"strings"
    10  )
    11  
    12  // Match identities on a OR basis, since nodes have only 1 identity
    13  func Match(filters []string, certname string) bool {
    14  	for _, filter := range filters {
    15  		if match(certname, filter) {
    16  			return true
    17  		}
    18  	}
    19  
    20  	return false
    21  }
    22  
    23  // FilterNodes return only nodes matching f
    24  func FilterNodes(nodes []string, f string) []string {
    25  	matched := []string{}
    26  
    27  	for _, n := range nodes {
    28  		if match(n, f) {
    29  			matched = append(matched, n)
    30  		}
    31  	}
    32  
    33  	return matched
    34  }
    35  
    36  func match(certname string, filter string) bool {
    37  	if strings.HasPrefix(filter, "/") && strings.HasSuffix(filter, "/") {
    38  		filter = strings.TrimPrefix(filter, "/")
    39  		filter = strings.TrimSuffix(filter, "/")
    40  		if matched, _ := regexp.MatchString(filter, certname); matched {
    41  			return true
    42  		}
    43  	}
    44  
    45  	return certname == filter
    46  }