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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#[macro_use]
extern crate log;

use std::str::FromStr;

use chrono::Utc;
use digest::Digest;
use hmac::{Hmac, Mac, NewMac};
use itertools::Itertools;
use md5::Md5;
use quick_protobuf::MessageWrite;
use reqwest::header::{
    HeaderValue, AUTHORIZATION, CONTENT_LENGTH, CONTENT_TYPE, DATE, HOST, USER_AGENT,
};
use reqwest::{Client, Method, RequestBuilder, Response, Url};
use sha1::Sha1;

pub use crate::error::{Error, Result};
use crate::model::LogGroup;

mod error;
mod model;
#[allow(dead_code)]
mod headers {
    pub const LOG_API_VERSION: &str = "x-log-apiversion";
    pub const LOG_SIGNATURE_METHOD: &str = "x-log-signaturemethod";
    pub const LOG_BODY_RAW_SIZE: &str = "x-log-bodyrawsize";
    pub const LOG_COMPRESS_TYPE: &str = "x-log-compresstype";
    pub const CONTENT_MD5: &str = "content-md5";
}
use headers::*;

#[cfg(test)]
mod test;

type HmacSha1 = Hmac<Sha1>;

pub const API_VERSION: &str = "0.6.0";
pub const SIGNATURE_METHOD: &str = "hmac-sha1";
pub const DEFAULT_CONTENT_TYPE: &str = "application/x-protobuf";
pub const USER_AGENT_VALUE: &str = concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION"));

pub struct LogProducer {
    access_key: Box<str>,
    access_secret: Box<str>,
    endpoint: Box<str>,
    host: Box<str>,
    logstore: Box<str>,
    client: Client,
}

impl LogProducer {
    pub fn new<K, S, E, P, L>(
        access_key: K,
        access_secret: S,
        endpoint: E,
        project: P,
        logstore: L,
    ) -> Result<Self>
    where
        K: AsRef<str>,
        S: AsRef<str>,
        E: AsRef<str>,
        P: AsRef<str>,
        L: AsRef<str>,
    {
        Ok(Self {
            access_key: access_key.as_ref().into(),
            access_secret: access_secret.as_ref().into(),
            endpoint: endpoint.as_ref().into(),
            host: format!("{}.{}", project.as_ref(), endpoint.as_ref()).into_boxed_str(),
            logstore: logstore.as_ref().into(),
            client: reqwest::ClientBuilder::new().build()?,
        })
    }

    /// POST /logstores/logstoreName/shards/lb
    pub async fn put_logs_lb(&self, log_group: &LogGroup<'_>) -> Result<Response> {
        let buf = log_group.encode()?;
        let request = self
            .new_request(
                Method::POST,
                format!("/logstores/{}/shards/lb", self.logstore),
            )?
            .header(LOG_BODY_RAW_SIZE, log_group.get_size())
            .body(buf);

        Ok(self.exec(request).await?)
    }

    fn new_request(&self, method: Method, path: String) -> Result<RequestBuilder> {
        let url = Url::from_str(&*format!("https://{}{}", self.endpoint, path))?;
        let date = Utc::now().format("%a,%d%b%Y %H:%M:%S GMT").to_string();
        debug!("created request on {}", date);
        let request = self
            .client
            .request(method, url)
            .header(USER_AGENT, USER_AGENT_VALUE)
            .header(DATE, date)
            .header(HOST, &*self.host)
            .header(LOG_API_VERSION, API_VERSION)
            .header(LOG_SIGNATURE_METHOD, SIGNATURE_METHOD);

        Ok(request)
    }

    async fn exec(&self, request: RequestBuilder) -> Result<reqwest::Response> {
        let mut request = request.build()?;

        let mut mac = HmacSha1::new_varkey(self.access_secret.as_bytes()).unwrap();
        // SignString = VERB + "\n"
        //              + CONTENT-MD5 + "\n"
        //              + CONTENT-TYPE + "\n"
        //              + DATE + "\n"
        //              + CanonicalizedLOGHeaders + "\n"
        //              + CanonicalizedResource
        let verb = request.method().as_str();
        debug!("-- method: {}", verb);
        mac.update(verb.as_bytes());
        mac.update(b"\n");

        if request.body().is_some() {
            debug!("-- body found");
            let body = request.body().unwrap().as_bytes().unwrap();
            let length = body.len();
            let digest = Md5::digest(body);
            let digest = hex::encode(digest).to_ascii_uppercase();
            debug!("-- content-md5: {}", digest);
            let md5 = request
                .headers_mut()
                .entry(CONTENT_MD5)
                .or_insert_with(|| digest.parse().unwrap());
            mac.update(md5.as_ref());
            mac.update(b"\n");

            // Add CONTENT_LENGTH header
            request.headers_mut().insert(CONTENT_LENGTH, length.into());

            let content_type = request
                .headers_mut()
                .entry(CONTENT_TYPE)
                .or_insert_with(|| HeaderValue::from_static(DEFAULT_CONTENT_TYPE));
            mac.update(content_type.as_ref());
            mac.update(b"\n");
        } else {
            mac.update(b"\n\n");
        }

        let date = request.headers_mut().entry(DATE).or_insert_with(|| {
            let date = Utc::now().format("%a, %d %b %Y %H:%M:%S GMT").to_string();
            date.parse().unwrap()
        });

        mac.update(date.as_ref());
        mac.update(b"\n");
        // CanonicalizedLOGHeaders的构造方式如下:
        //     将所有以x-log和x-acs为前缀的HTTP请求头的名字转换成小写字母。
        //     将上一步得到的所有LOG自定义请求头按照字典顺序进行升序排序。
        //     删除请求头和内容之间分隔符两端出现的任何空格。
        //     将所有的头和内容用\n分隔符组合成最后的CanonicalizedLOGHeader。
        request
            .headers()
            .iter()
            .filter(|(key, _)| {
                key.as_str().starts_with("x-log") || key.as_str().starts_with("x-acs")
            })
            .sorted_by_key(|(key, _)| key.as_str())
            .map(|(key, value)| {
                format!(
                    "{}:{}",
                    key.as_str().to_ascii_lowercase(),
                    value.to_str().unwrap()
                )
            })
            .for_each(|next| {
                debug!("-- header: {}", next);
                mac.update(next.as_bytes());
                mac.update(b"\n");
            });

        // CanonicalizedResource的构造方式如下:
        // a. 将CanonicalizedResource设置为空字符串" "。
        // b. 放入要访问的LOG资源,如/logstores/logstorename(如果没有logstorename则可不填写)。
        // c. 如果请求包含查询字符串QUERY_STRING,则在CanonicalizedResource字符串尾部添加?和查询字符串。
        //
        // QUERY_STRING是URL中请求参数按字典顺序排序后的字符串,其中参数名和值之间用=相隔组成字符串,并对参数名-值对按照字典顺序升序排序,然后以&符号连接构成字符串。其公式化描述如下:
        // QUERY_STRING = "KEY1=VALUE1" + "&" + "KEY2=VALUE2"

        let url = request.url();
        let path = url.path();
        debug!("-- path: {}", path);
        debug!("-- query: {:?}", url.query());
        mac.update(path.as_bytes());
        if url.query().is_some() {
            mac.update(b"?");
            let query_string = url
                .query_pairs()
                .map(|(key, value)| format!("{}={}", key, value))
                .sorted()
                .join("&");
            mac.update(query_string.as_bytes());
        }

        let authorization = base64::encode(mac.finalize().into_bytes());
        let authorization = format!("LOG {}:{}", self.access_key, authorization);
        request
            .headers_mut()
            .insert(AUTHORIZATION, authorization.parse().unwrap());

        Ok(self.client.execute(request).await?)
    }
}