github.com/rigado/snapd@v2.42.5-go-mod+incompatible/overlord/configstate/configcore/proxy.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2017 Canonical Ltd
     5   *
     6   * This program is free software: you can redistribute it and/or modify
     7   * it under the terms of the GNU General Public License version 3 as
     8   * published by the Free Software Foundation.
     9   *
    10   * This program is distributed in the hope that it will be useful,
    11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13   * GNU General Public License for more details.
    14   *
    15   * You should have received a copy of the GNU General Public License
    16   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17   *
    18   */
    19  
    20  package configcore
    21  
    22  import (
    23  	"fmt"
    24  	"io/ioutil"
    25  	"os"
    26  	"path/filepath"
    27  	"strings"
    28  
    29  	"github.com/snapcore/snapd/asserts"
    30  	"github.com/snapcore/snapd/dirs"
    31  	"github.com/snapcore/snapd/overlord/assertstate"
    32  	"github.com/snapcore/snapd/overlord/configstate/config"
    33  )
    34  
    35  var proxyConfigKeys = map[string]bool{
    36  	"http_proxy":  true,
    37  	"https_proxy": true,
    38  	"ftp_proxy":   true,
    39  	"no_proxy":    true,
    40  }
    41  
    42  func init() {
    43  	// add supported configuration of this module
    44  	supportedConfigurations["core.proxy.http"] = true
    45  	supportedConfigurations["core.proxy.https"] = true
    46  	supportedConfigurations["core.proxy.ftp"] = true
    47  	supportedConfigurations["core.proxy.no-proxy"] = true
    48  	supportedConfigurations["core.proxy.store"] = true
    49  }
    50  
    51  func etcEnvironment() string {
    52  	return filepath.Join(dirs.GlobalRootDir, "/etc/environment")
    53  }
    54  
    55  func updateEtcEnvironmentConfig(path string, config map[string]string) error {
    56  	f, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE, 0644)
    57  	if err != nil {
    58  		return nil
    59  	}
    60  	defer f.Close()
    61  
    62  	toWrite, err := updateKeyValueStream(f, proxyConfigKeys, config)
    63  	if err != nil {
    64  		return err
    65  	}
    66  	if toWrite != nil {
    67  		return ioutil.WriteFile(path, []byte(strings.Join(toWrite, "\n")), 0644)
    68  	}
    69  
    70  	return nil
    71  }
    72  
    73  func handleProxyConfiguration(tr config.Conf) error {
    74  	config := map[string]string{}
    75  	// normal proxy settings
    76  	for _, key := range []string{"http", "https", "ftp"} {
    77  		output, err := coreCfg(tr, "proxy."+key)
    78  		if err != nil {
    79  			return err
    80  		}
    81  		config[key+"_proxy"] = output
    82  	}
    83  	// handle no_proxy
    84  	output, err := coreCfg(tr, "proxy.no-proxy")
    85  	if err != nil {
    86  		return err
    87  	}
    88  	config["no_proxy"] = output
    89  
    90  	if err := updateEtcEnvironmentConfig(etcEnvironment(), config); err != nil {
    91  		return err
    92  	}
    93  
    94  	return nil
    95  }
    96  
    97  func validateProxyStore(tr config.Conf) error {
    98  	proxyStore, err := coreCfg(tr, "proxy.store")
    99  	if err != nil {
   100  		return err
   101  	}
   102  
   103  	if proxyStore == "" {
   104  		return nil
   105  	}
   106  
   107  	st := tr.State()
   108  	st.Lock()
   109  	defer st.Unlock()
   110  
   111  	store, err := assertstate.Store(st, proxyStore)
   112  	if asserts.IsNotFound(err) {
   113  		return fmt.Errorf("cannot set proxy.store to %q without a matching store assertion", proxyStore)
   114  	}
   115  	if err == nil && store.URL() == nil {
   116  		return fmt.Errorf("cannot set proxy.store to %q with a matching store assertion with url unset", proxyStore)
   117  	}
   118  	return err
   119  }