github.com/zntrio/harp/v2@v2.0.9/pkg/bundle/template/visitor/secretbuilder/platform.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 secretbuilder
    19  
    20  import (
    21  	"fmt"
    22  	"strings"
    23  
    24  	bundlev1 "github.com/zntrio/harp/v2/api/gen/go/harp/bundle/v1"
    25  	"github.com/zntrio/harp/v2/pkg/bundle/template/visitor"
    26  	csov1 "github.com/zntrio/harp/v2/pkg/cso/v1"
    27  	"github.com/zntrio/harp/v2/pkg/template/engine"
    28  )
    29  
    30  type platformSecretBuilder struct {
    31  	results         chan *bundlev1.Package
    32  	templateContext engine.Context
    33  
    34  	// Context
    35  	quality   string
    36  	name      string
    37  	region    string
    38  	component string
    39  	err       error
    40  }
    41  
    42  // -----------------------------------------------------------------------------
    43  
    44  // Infrastructure returns a visitor instance to generate secretpath
    45  // and values.
    46  func platform(results chan *bundlev1.Package, templateContext engine.Context, quality, name string) (visitor.PlatformVisitor, error) {
    47  	// Parse selector values
    48  	platformQuality, err := engine.RenderContext(templateContext, quality)
    49  	if err != nil {
    50  		return nil, fmt.Errorf("unable to render platform.quality: %w", err)
    51  	}
    52  	if strings.TrimSpace(platformQuality) == "" {
    53  		return nil, fmt.Errorf("quality selector must not be empty")
    54  	}
    55  
    56  	platformName, err := engine.RenderContext(templateContext, name)
    57  	if err != nil {
    58  		return nil, fmt.Errorf("unable to render platform.name: %w", err)
    59  	}
    60  	if strings.TrimSpace(platformName) == "" {
    61  		return nil, fmt.Errorf("platform selector must not be empty")
    62  	}
    63  
    64  	return &platformSecretBuilder{
    65  		results:         results,
    66  		templateContext: templateContext,
    67  		quality:         platformQuality,
    68  		name:            platformName,
    69  	}, nil
    70  }
    71  
    72  // -----------------------------------------------------------------------------
    73  
    74  func (b *platformSecretBuilder) Error() error {
    75  	return b.err
    76  }
    77  
    78  func (b *platformSecretBuilder) VisitForRegion(obj *bundlev1.PlatformRegionNS) {
    79  	// Check arguments
    80  	if obj == nil {
    81  		return
    82  	}
    83  
    84  	// Set context values
    85  	b.region, b.err = engine.RenderContext(b.templateContext, obj.Region)
    86  	if b.err != nil {
    87  		return
    88  	}
    89  
    90  	// Iterates over all components
    91  	for _, item := range obj.Components {
    92  		b.VisitForComponent(item)
    93  	}
    94  }
    95  
    96  func (b *platformSecretBuilder) VisitForComponent(obj *bundlev1.PlatformComponentNS) {
    97  	// Check arguments
    98  	if obj == nil {
    99  		return
   100  	}
   101  
   102  	// Set context value
   103  	b.component, b.err = engine.RenderContext(b.templateContext, obj.Name)
   104  	if b.err != nil {
   105  		return
   106  	}
   107  
   108  	for _, item := range obj.Secrets {
   109  		// Check arguments
   110  		if item == nil {
   111  			continue
   112  		}
   113  
   114  		// Parse suffix with template engine
   115  		suffix, err := engine.RenderContext(b.templateContext, item.Suffix)
   116  		if err != nil {
   117  			b.err = fmt.Errorf("unable to merge template is suffix %q", item.Suffix)
   118  			return
   119  		}
   120  
   121  		// Generate secret suffix
   122  		secretPath, err := csov1.RingPlatform.Path(b.quality, b.name, b.region, b.component, suffix)
   123  		if err != nil {
   124  			b.err = err
   125  			return
   126  		}
   127  
   128  		// Prepare template model
   129  		tmplModel := &struct {
   130  			Quality   string
   131  			Name      string
   132  			Region    string
   133  			Component string
   134  			Secret    *bundlev1.SecretSuffix
   135  		}{
   136  			Quality:   b.quality,
   137  			Name:      b.name,
   138  			Region:    b.region,
   139  			Component: b.component,
   140  			Secret:    item,
   141  		}
   142  
   143  		// Compile template
   144  		p, err := parseSecretTemplate(b.templateContext, secretPath, item, tmplModel)
   145  		if err != nil {
   146  			b.err = err
   147  			return
   148  		}
   149  
   150  		// Add package to collection
   151  		b.results <- p
   152  	}
   153  }