use std::fmt;
use std::path::PathBuf;
use std::str::FromStr;
use std::sync::Arc;
use std::time::Duration;
use crate::core::{
CommonConfig, CommonConfigBuilder, EnvReader, Error, Hooks, Result, RetryPolicy, Secret,
TlsConfig,
};
pub const DEFAULT_URL: &str = "http://127.0.0.1:9000";
pub const DEFAULT_GELF_PORT: u16 = 12201;
pub const DEFAULT_GELF_HTTP_PORT: u16 = 12202;
pub const DEFAULT_OTLP_GRPC_PORT: u16 = 4317;
pub const DEFAULT_REQUESTED_BY: &str = "grow-codes-graylog";
const VENDOR_URL_VARS: &[&str] = &["GRAYLOG_URL"];
const VENDOR_API_TOKEN_VARS: &[&str] = &["GRAYLOG_API_TOKEN"];
const VENDOR_USER_VARS: &[&str] = &["GRAYLOG_USER"];
const VENDOR_PASSWORD_VARS: &[&str] = &["GRAYLOG_PASSWORD"];
const VENDOR_GELF_HOST_VARS: &[&str] = &["GRAYLOG_GELF_HOST", "GRAYLOG_HOST"];
const VENDOR_GELF_PORT_VARS: &[&str] = &["GRAYLOG_GELF_PORT", "GRAYLOG_PORT"];
const VENDOR_GELF_PROTOCOL_VARS: &[&str] = &["GRAYLOG_GELF_PROTOCOL", "GRAYLOG_TRANSPORT"];
const VENDOR_GELF_HTTP_URL_VARS: &[&str] = &["GRAYLOG_GELF_HTTP_URL"];
const VENDOR_OTLP_ENDPOINT_VARS: &[&str] = &["GRAYLOG_OTLP_GRPC_ENDPOINT"];
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum GelfProtocol {
Udp,
#[default]
Tcp,
Http,
Otlp,
}
impl GelfProtocol {
pub fn as_str(self) -> &'static str {
match self {
GelfProtocol::Udp => "udp",
GelfProtocol::Tcp => "tcp",
GelfProtocol::Http => "http",
GelfProtocol::Otlp => "otlp",
}
}
pub fn is_gelf(self) -> bool {
self != GelfProtocol::Otlp
}
}
impl fmt::Display for GelfProtocol {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
impl FromStr for GelfProtocol {
type Err = Error;
fn from_str(s: &str) -> Result<Self> {
match s.trim().to_ascii_lowercase().as_str() {
"udp" => Ok(GelfProtocol::Udp),
"tcp" => Ok(GelfProtocol::Tcp),
"http" => Ok(GelfProtocol::Http),
"otlp" => Ok(GelfProtocol::Otlp),
_ => Err(Error::validation("protocol must be udp, tcp, http or otlp")),
}
}
}
#[derive(Clone)]
pub struct GraylogConfig {
common: CommonConfig,
api_token: Option<Secret>,
username: Option<String>,
password: Option<Secret>,
gelf_host: String,
gelf_port: Option<u16>,
gelf_protocol: GelfProtocol,
gelf_http_url: Option<String>,
gelf_tls: bool,
otlp_endpoint: Option<String>,
requested_by: String,
}
impl fmt::Debug for GraylogConfig {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("GraylogConfig")
.field("common", &self.common)
.field("api_token", &self.api_token)
.field("username", &self.username)
.field("password", &self.password)
.field("gelf_host", &self.gelf_host)
.field("gelf_port", &self.gelf_port)
.field("gelf_protocol", &self.gelf_protocol)
.field(
"gelf_http_url",
&self
.gelf_http_url
.as_deref()
.map(crate::core::redact::redact_url),
)
.field("gelf_tls", &self.gelf_tls)
.field(
"otlp_endpoint",
&self
.otlp_endpoint
.as_deref()
.map(crate::core::redact::redact_url),
)
.field("requested_by", &self.requested_by)
.finish()
}
}
impl GraylogConfig {
pub fn builder() -> GraylogConfigBuilder {
GraylogConfigBuilder::default()
}
pub fn from_env() -> Result<Self> {
Self::builder().build_from_env()
}
pub fn common(&self) -> &CommonConfig {
&self.common
}
pub fn url(&self) -> &url::Url {
self.common.url()
}
pub fn api_token(&self) -> Option<&Secret> {
self.api_token.as_ref()
}
pub fn username(&self) -> Option<&str> {
self.username.as_deref()
}
pub fn password(&self) -> Option<&Secret> {
self.password.as_ref()
}
pub fn gelf_host(&self) -> &str {
&self.gelf_host
}
pub fn gelf_port(&self) -> Option<u16> {
self.gelf_port
}
pub fn gelf_protocol(&self) -> GelfProtocol {
self.gelf_protocol
}
pub fn gelf_http_url(&self) -> Option<&str> {
self.gelf_http_url.as_deref()
}
pub fn gelf_tls(&self) -> bool {
self.gelf_tls
}
pub fn otlp_endpoint(&self) -> Option<&str> {
self.otlp_endpoint.as_deref()
}
pub fn requested_by(&self) -> &str {
&self.requested_by
}
pub fn gelf_port_for(&self, protocol: GelfProtocol) -> u16 {
match (self.gelf_port, protocol) {
(Some(p), _) => p,
(None, GelfProtocol::Http) => DEFAULT_GELF_HTTP_PORT,
(None, _) => DEFAULT_GELF_PORT,
}
}
pub fn gelf_http_endpoint(&self) -> String {
if let Some(u) = &self.gelf_http_url {
return u.clone();
}
let scheme = if self.gelf_tls { "https" } else { "http" };
format!(
"{scheme}://{}:{}/gelf",
url_host(&self.gelf_host),
self.gelf_port_for(GelfProtocol::Http)
)
}
pub fn otlp_grpc_endpoint(&self) -> String {
match &self.otlp_endpoint {
Some(e) => e.clone(),
None => format!(
"http://{}:{DEFAULT_OTLP_GRPC_PORT}",
url_host(&self.gelf_host)
),
}
}
pub fn validate(&self) -> Result<()> {
if !self.gelf_protocol.is_gelf() {
return Err(Error::config("gelfProtocol must be udp, tcp or http"));
}
if self.gelf_port == Some(0) {
return Err(Error::config("gelfPort must be an integer 1..65535"));
}
if self.gelf_host.trim().is_empty() {
return Err(Error::config("gelfHost must not be blank"));
}
if let Some(u) = &self.gelf_http_url {
if !is_http_url(u) {
return Err(Error::config("gelfHttpUrl must be an http(s) URL"));
}
}
if let Some(u) = &self.otlp_endpoint {
if !is_http_url(u) {
return Err(Error::config("otlpEndpoint must be an http(s) URL"));
}
}
if let Some(t) = &self.api_token {
if t.expose_secret().trim().is_empty() {
return Err(Error::config("apiToken must not be blank"));
}
}
if self.username.is_some() != self.password.is_some() {
return Err(Error::config(
"username and password must be given together",
));
}
if self.requested_by.trim().is_empty() {
return Err(Error::config("requestedBy must not be blank"));
}
Ok(())
}
}
pub(crate) fn url_host(host: &str) -> String {
if host.contains(':') && !host.starts_with('[') {
format!("[{host}]")
} else {
host.to_owned()
}
}
pub(crate) fn is_http_url(s: &str) -> bool {
url::Url::parse(s)
.map(|u| matches!(u.scheme(), "http" | "https") && u.host_str().is_some())
.unwrap_or(false)
}
#[derive(Clone, Default)]
pub struct GraylogConfigBuilder {
common: CommonConfigBuilder,
api_token: Option<Secret>,
username: Option<String>,
password: Option<Secret>,
gelf_host: Option<String>,
gelf_port: Option<u16>,
gelf_protocol: Option<GelfProtocol>,
gelf_http_url: Option<String>,
gelf_tls: Option<bool>,
otlp_endpoint: Option<String>,
requested_by: Option<String>,
}
impl fmt::Debug for GraylogConfigBuilder {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("GraylogConfigBuilder")
.field("common", &self.common)
.field("api_token", &self.api_token)
.field("username", &self.username)
.field("password", &self.password)
.field("gelf_host", &self.gelf_host)
.field("gelf_port", &self.gelf_port)
.field("gelf_protocol", &self.gelf_protocol)
.finish_non_exhaustive()
}
}
impl GraylogConfigBuilder {
pub fn url(mut self, url: impl Into<String>) -> Self {
self.common = self.common.url(url);
self
}
pub fn api_token(mut self, token: impl Into<Secret>) -> Self {
self.api_token = Some(token.into());
self
}
pub fn credentials(mut self, username: impl Into<String>, password: impl Into<Secret>) -> Self {
self.username = Some(username.into());
self.password = Some(password.into());
self
}
pub fn username(mut self, username: impl Into<String>) -> Self {
self.username = Some(username.into());
self
}
pub fn password(mut self, password: impl Into<Secret>) -> Self {
self.password = Some(password.into());
self
}
pub fn gelf_host(mut self, host: impl Into<String>) -> Self {
self.gelf_host = Some(host.into());
self
}
pub fn gelf_port(mut self, port: u16) -> Self {
self.gelf_port = Some(port);
self
}
pub fn gelf_protocol(mut self, protocol: GelfProtocol) -> Self {
self.gelf_protocol = Some(protocol);
self
}
pub fn gelf_http_url(mut self, url: impl Into<String>) -> Self {
self.gelf_http_url = Some(url.into());
self
}
pub fn gelf_tls(mut self, tls: bool) -> Self {
self.gelf_tls = Some(tls);
self
}
pub fn otlp_endpoint(mut self, endpoint: impl Into<String>) -> Self {
self.otlp_endpoint = Some(endpoint.into());
self
}
pub fn requested_by(mut self, value: impl Into<String>) -> Self {
self.requested_by = Some(value.into());
self
}
pub fn connect_timeout(mut self, timeout: Duration) -> Self {
self.common = self.common.connect_timeout(timeout);
self
}
pub fn request_timeout(mut self, timeout: Duration) -> Self {
self.common = self.common.request_timeout(timeout);
self
}
pub fn total_timeout(mut self, timeout: Duration) -> Self {
self.common = self.common.total_timeout(timeout);
self
}
pub fn retry(mut self, retry: RetryPolicy) -> Self {
self.common = self.common.retry(retry);
self
}
pub fn max_retries(mut self, max_retries: u32) -> Self {
self.common = self.common.max_retries(max_retries);
self
}
pub fn tls(mut self, tls: TlsConfig) -> Self {
self.common = self.common.tls(tls);
self
}
pub fn ca_cert_path(mut self, path: impl Into<PathBuf>) -> Self {
self.common = self.common.ca_cert_path(path);
self
}
pub fn insecure_skip_verify(mut self, skip: bool) -> Self {
self.common = self.common.insecure_skip_verify(skip);
self
}
pub fn proxy(mut self, proxy: impl Into<String>) -> Self {
self.common = self.common.proxy(proxy);
self
}
pub fn header(mut self, name: impl Into<String>, value: impl Into<String>) -> Self {
self.common = self.common.header(name, value);
self
}
pub fn user_agent_suffix(mut self, suffix: impl Into<String>) -> Self {
self.common = self.common.user_agent_suffix(suffix);
self
}
pub fn hooks(mut self, hooks: Arc<dyn Hooks>) -> Self {
self.common = self.common.hooks(hooks);
self
}
pub fn common(mut self, f: impl FnOnce(CommonConfigBuilder) -> CommonConfigBuilder) -> Self {
self.common = f(self.common);
self
}
pub fn build(self) -> Result<GraylogConfig> {
let common = self.common.build(DEFAULT_URL)?;
let gelf_host = match self.gelf_host {
Some(h) => h
.trim()
.trim_start_matches('[')
.trim_end_matches(']')
.to_owned(),
None => common
.url()
.host_str()
.unwrap_or("127.0.0.1")
.trim_start_matches('[')
.trim_end_matches(']')
.to_owned(),
};
let config = GraylogConfig {
common,
api_token: self.api_token,
username: self.username,
password: self.password,
gelf_host,
gelf_port: self.gelf_port,
gelf_protocol: self.gelf_protocol.unwrap_or_default(),
gelf_http_url: self.gelf_http_url,
gelf_tls: self.gelf_tls.unwrap_or(false),
otlp_endpoint: self.otlp_endpoint,
requested_by: self
.requested_by
.unwrap_or_else(|| DEFAULT_REQUESTED_BY.to_owned()),
};
config.validate()?;
Ok(config)
}
pub fn build_from_env(self) -> Result<GraylogConfig> {
self.build_with_env(&EnvReader::new())
}
pub fn build_with_env(mut self, env: &EnvReader) -> Result<GraylogConfig> {
self.common = self.common.apply_env(env, VENDOR_URL_VARS)?;
if self.api_token.is_none() {
self.api_token = env.secret("API_TOKEN", VENDOR_API_TOKEN_VARS);
}
if self.username.is_none() {
self.username = env.string("USER", VENDOR_USER_VARS);
}
if self.password.is_none() {
self.password = env.secret("PASSWORD", VENDOR_PASSWORD_VARS);
}
if self.gelf_host.is_none() {
self.gelf_host = env.string("GELF_HOST", VENDOR_GELF_HOST_VARS);
}
if self.gelf_http_url.is_none() {
self.gelf_http_url = env.string("GELF_HTTP_URL", VENDOR_GELF_HTTP_URL_VARS);
}
if self.otlp_endpoint.is_none() {
self.otlp_endpoint = env.string("OTLP_ENDPOINT", VENDOR_OTLP_ENDPOINT_VARS);
}
if self.gelf_protocol.is_none() {
if let Some((var, value)) = env.lookup("GELF_PROTOCOL", VENDOR_GELF_PROTOCOL_VARS) {
match value.parse::<GelfProtocol>() {
Ok(p) if p.is_gelf() => self.gelf_protocol = Some(p),
_ => {
return Err(Error::config(format!(
"invalid value in environment variable {var}: expected udp, tcp or http"
)))
}
}
}
}
if self.gelf_tls.is_none() {
self.gelf_tls = env.bool("GELF_TLS", &[])?;
}
if self.gelf_port.is_none() {
if let Some(n) = env.int("GELF_PORT", VENDOR_GELF_PORT_VARS)? {
let port = u16::try_from(n).ok().filter(|p| *p > 0).ok_or_else(|| {
let var = env
.lookup("GELF_PORT", VENDOR_GELF_PORT_VARS)
.map(|(v, _)| v)
.unwrap_or_default();
Error::config(format!(
"environment variable {var} must be an integer 1..65535"
))
})?;
self.gelf_port = Some(port);
}
}
self.build()
}
}