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