dubbo.apache.org/dubbo-go/v3@v3.1.1/xds/credentials/certprovider/pemfile/builder.go (about)

     1  /*
     2   * Licensed to the Apache Software Foundation (ASF) under one or more
     3   * contributor license agreements.  See the NOTICE file distributed with
     4   * this work for additional information regarding copyright ownership.
     5   * The ASF licenses this file to You under the Apache License, Version 2.0
     6   * (the "License"); you may not use this file except in compliance with
     7   * the License.  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, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17  
    18  /*
    19   *
    20   * Copyright 2020 gRPC authors.
    21   *
    22   */
    23  
    24  package pemfile
    25  
    26  import (
    27  	"encoding/json"
    28  	"fmt"
    29  	"time"
    30  )
    31  
    32  import (
    33  	"google.golang.org/protobuf/encoding/protojson"
    34  
    35  	"google.golang.org/protobuf/types/known/durationpb"
    36  )
    37  
    38  import (
    39  	"dubbo.apache.org/dubbo-go/v3/xds/credentials/certprovider"
    40  )
    41  
    42  const (
    43  	pluginName             = "file_watcher"
    44  	defaultRefreshInterval = 10 * time.Minute
    45  )
    46  
    47  type PluginBuilder struct{}
    48  
    49  func (p *PluginBuilder) ParseConfig(c interface{}) (*certprovider.BuildableConfig, error) {
    50  	data, ok := c.(json.RawMessage)
    51  	if !ok {
    52  		return nil, fmt.Errorf("meshca: unsupported config type: %T", c)
    53  	}
    54  	opts, err := pluginConfigFromJSON(data)
    55  	if err != nil {
    56  		return nil, err
    57  	}
    58  	return certprovider.NewBuildableConfig(pluginName, opts.canonical(), func(certprovider.BuildOptions) certprovider.Provider {
    59  		return newProvider(opts)
    60  	}), nil
    61  }
    62  
    63  func (p *PluginBuilder) Name() string {
    64  	return pluginName
    65  }
    66  
    67  func pluginConfigFromJSON(jd json.RawMessage) (Options, error) {
    68  	// The only difference between this anonymous struct and the Options struct
    69  	// is that the refresh_interval is represented here as a duration proto,
    70  	// while in the latter a time.Duration is used.
    71  	cfg := &struct {
    72  		CertificateFile   string          `json:"certificate_file,omitempty"`
    73  		PrivateKeyFile    string          `json:"private_key_file,omitempty"`
    74  		CACertificateFile string          `json:"ca_certificate_file,omitempty"`
    75  		RefreshInterval   json.RawMessage `json:"refresh_interval,omitempty"`
    76  	}{}
    77  	if err := json.Unmarshal(jd, cfg); err != nil {
    78  		return Options{}, fmt.Errorf("pemfile: json.Unmarshal(%s) failed: %v", string(jd), err)
    79  	}
    80  
    81  	opts := Options{
    82  		CertFile: cfg.CertificateFile,
    83  		KeyFile:  cfg.PrivateKeyFile,
    84  		RootFile: cfg.CACertificateFile,
    85  		// Refresh interval is the only field in the configuration for which we
    86  		// support a default value. We cannot possibly have valid defaults for
    87  		// file paths to watch. Also, it is valid to specify an empty path for
    88  		// some of those fields if the user does not want to watch them.
    89  		RefreshDuration: defaultRefreshInterval,
    90  	}
    91  	if cfg.RefreshInterval != nil {
    92  		dur := &durationpb.Duration{}
    93  		if err := protojson.Unmarshal(cfg.RefreshInterval, dur); err != nil {
    94  			return Options{}, fmt.Errorf("pemfile: protojson.Unmarshal(%+v) failed: %v", cfg.RefreshInterval, err)
    95  		}
    96  		opts.RefreshDuration = dur.AsDuration()
    97  	}
    98  	fmt.Println("pluginConfigFromJSON")
    99  	fmt.Println(opts)
   100  
   101  	if err := opts.validate(); err != nil {
   102  		return Options{}, err
   103  	}
   104  	return opts, nil
   105  }