conjure_runtime_raw/service/proxy/
mod.rs

1// Copyright 2020 Palantir Technologies, Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14use crate::config;
15pub(crate) use crate::service::proxy::connector::ProxyConnectorService;
16use base64::display::Base64Display;
17use base64::engine::general_purpose::STANDARD;
18use conjure_error::Error;
19use http::{HeaderValue, Uri};
20use std::convert::TryFrom;
21
22pub mod connector;
23
24#[derive(Clone)]
25pub enum ProxyConfig {
26    Http(HttpProxyConfig),
27    Direct,
28}
29
30#[derive(Clone)]
31pub struct HttpProxyConfig {
32    uri: Uri,
33    credentials: Option<HeaderValue>,
34}
35
36impl ProxyConfig {
37    pub fn from_config(config: &config::ProxyConfig) -> Result<ProxyConfig, Error> {
38        match config {
39            config::ProxyConfig::Direct => Ok(ProxyConfig::Direct),
40            config::ProxyConfig::Http(config) => {
41                let uri = format!(
42                    "http://{}:{}",
43                    config.host_and_port().host(),
44                    config.host_and_port().port(),
45                )
46                .parse::<Uri>()
47                .map_err(Error::internal_safe)?;
48
49                let credentials = config.credentials().map(|c| {
50                    let auth = format!("{}:{}", c.username(), c.password());
51                    let header =
52                        format!("Basic {}", Base64Display::new(auth.as_bytes(), &STANDARD));
53                    HeaderValue::try_from(header).unwrap()
54                });
55
56                Ok(ProxyConfig::Http(HttpProxyConfig { uri, credentials }))
57            }
58            _ => Err(Error::internal_safe("unknown proxy type")),
59        }
60    }
61}