github.com/zntrio/harp/v2@v2.0.9/pkg/bundle/kv.go (about)

     1  // Licensed to Elasticsearch B.V. under one or more contributor
     2  // license agreements. See the NOTICE file distributed with
     3  // this work for additional information regarding copyright
     4  // ownership. Elasticsearch B.V. licenses this file to you under
     5  // the Apache License, Version 2.0 (the "License"); you may
     6  // not use this file except in compliance with the License.
     7  // You may obtain a copy of the License at
     8  //
     9  //     http://www.apache.org/licenses/LICENSE-2.0
    10  //
    11  // Unless required by applicable law or agreed to in writing,
    12  // software distributed under the License is distributed on an
    13  // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    14  // KIND, either express or implied.  See the License for the
    15  // specific language governing permissions and limitations
    16  // under the License.
    17  
    18  package bundle
    19  
    20  import (
    21  	"errors"
    22  	"fmt"
    23  
    24  	"github.com/gobwas/glob"
    25  
    26  	bundlev1 "github.com/zntrio/harp/v2/api/gen/go/harp/bundle/v1"
    27  	"github.com/zntrio/harp/v2/pkg/bundle/secret"
    28  )
    29  
    30  // KV describes map[string]interface{} alias.
    31  type KV map[string]interface{}
    32  
    33  // Glob returns package objects that have name matching the given pattern.
    34  func (kv KV) Glob(pattern string) KV {
    35  	// Prepare Glob filter.
    36  	g, err := glob.Compile(pattern, '/')
    37  	if err != nil {
    38  		g, _ = glob.Compile("**")
    39  	}
    40  
    41  	// Apply to collection
    42  	nkv := KV{}
    43  	for name, contents := range kv {
    44  		if g.Match(name) {
    45  			nkv[name] = contents
    46  		}
    47  	}
    48  
    49  	return nkv
    50  }
    51  
    52  // Get returns a KV of the given package.
    53  func (kv KV) Get(name string) interface{} {
    54  	if v, ok := kv[name]; ok {
    55  		return v
    56  	}
    57  	return KV{}
    58  }
    59  
    60  // -----------------------------------------------------------------------------
    61  
    62  // AsSecretMap returns a KV map from given package.
    63  func AsSecretMap(p *bundlev1.Package) (KV, error) {
    64  	// Check arguments
    65  	if p == nil {
    66  		return nil, errors.New("unable to transform nil package")
    67  	}
    68  
    69  	secrets := KV{}
    70  	for _, s := range p.Secrets.Data {
    71  		// Unpack secret value
    72  		var data interface{}
    73  		if err := secret.Unpack(s.Value, &data); err != nil {
    74  			return nil, fmt.Errorf("unable to unpack %q secret value: %w", p.Name, err)
    75  		}
    76  
    77  		// Assign result
    78  		secrets[s.Key] = data
    79  	}
    80  
    81  	// No error
    82  	return secrets, nil
    83  }
    84  
    85  // FromSecretMap returns the protobuf representation of secretMap.
    86  func FromSecretMap(secretKv KV) ([]*bundlev1.KV, error) {
    87  	secrets := []*bundlev1.KV{}
    88  
    89  	// Prepare secret data
    90  	for k, v := range secretKv {
    91  		// Pack secret value
    92  		packed, err := secret.Pack(v)
    93  		if err != nil {
    94  			return nil, fmt.Errorf("unable to pack secret value for `%s`: %w", k, err)
    95  		}
    96  
    97  		// Add to secret package
    98  		secrets = append(secrets, &bundlev1.KV{
    99  			Key:   k,
   100  			Type:  fmt.Sprintf("%T", v),
   101  			Value: packed,
   102  		})
   103  	}
   104  
   105  	// No error
   106  	return secrets, nil
   107  }