cx_sdk/client/
saml.rs

1// Copyright 2024 Coralogix Ltd.
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//	https://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.
14
15use std::str::FromStr;
16
17use cx_api::proto::com::coralogixapis::aaa::sso::v2::{
18    GetConfigurationRequest,
19    GetConfigurationResponse,
20    GetSpParametersRequest,
21    GetSpParametersResponse,
22    SetActiveRequest,
23    SetIdpParametersRequest,
24    saml_configuration_service_client::SamlConfigurationServiceClient,
25};
26
27pub use cx_api::proto::com::coralogixapis::aaa::sso::v2::{
28    IdpParameters,
29    idp_parameters::Metadata,
30};
31use tokio::sync::Mutex;
32use tonic::{
33    metadata::MetadataMap,
34    transport::{
35        Channel,
36        ClientTlsConfig,
37        Endpoint,
38    },
39};
40
41use crate::{
42    CoralogixRegion,
43    auth::AuthContext,
44    error::{
45        Result,
46        SdkApiError,
47        SdkError,
48    },
49    metadata::CallProperties,
50    util::make_request_with_metadata,
51};
52
53const SAML_FEATURE_GROUP_ID: &str = "aaa";
54/// A client for the SAML Configuration API.
55/// Read more at <https://coralogix.com/docs/slo-management-api/>
56pub struct SamlClient {
57    metadata_map: MetadataMap,
58    service_client: Mutex<SamlConfigurationServiceClient<Channel>>,
59}
60
61impl SamlClient {
62    /// Creates a new client for the SAML Configuration.
63    ///
64    /// # Arguments
65    /// * `auth_context` - The [`AuthContext`] to use for authentication.
66    /// * `region` - The [`CoralogixRegion`] to connect to.
67    pub fn new(auth_context: AuthContext, region: CoralogixRegion) -> Result<Self> {
68        let channel: Channel = Endpoint::from_str(region.grpc_endpoint().as_str())?
69            .tls_config(ClientTlsConfig::new().with_native_roots())?
70            .connect_lazy();
71        let request_metadata: CallProperties = (&auth_context.team_level_api_key).into();
72        Ok(Self {
73            metadata_map: request_metadata.to_metadata_map(),
74            service_client: Mutex::new(SamlConfigurationServiceClient::new(channel)),
75        })
76    }
77
78    /// Retrieves the SP parameters for a given team.
79    ///
80    /// # Arguments
81    /// * `team_id` - The ID of the team to retrieve parameters for.
82    pub async fn get_sp_parameters(&self, team_id: u32) -> Result<GetSpParametersResponse> {
83        let request =
84            make_request_with_metadata(GetSpParametersRequest { team_id }, &self.metadata_map);
85        self.service_client
86            .lock()
87            .await
88            .get_sp_parameters(request)
89            .await
90            .map(|r| r.into_inner())
91            .map_err(|status| {
92                SdkError::ApiError(SdkApiError {
93                    status,
94                    endpoint: "/com.coralogix.aaa.sso.v2.SamlConfigurationService/GetSpParameters"
95                        .to_string(),
96                    feature_group: SAML_FEATURE_GROUP_ID.into(),
97                })
98            })
99    }
100
101    /// Sets the IDP parameters for a given team.
102    ///
103    /// # Arguments
104    /// * `team_id` - The ID of the team to set parameters for.
105    /// * `idp_parameters` - The [`IdpParameters`] to set.
106    pub async fn set_idp_parameters(
107        &self,
108        team_id: u32,
109        idp_parameters: Option<IdpParameters>,
110    ) -> Result<()> {
111        let request = make_request_with_metadata(
112            SetIdpParametersRequest {
113                team_id,
114                params: idp_parameters,
115            },
116            &self.metadata_map,
117        );
118        self.service_client
119            .lock()
120            .await
121            .set_idp_parameters(request)
122            .await
123            .map(|_| ())
124            .map_err(|status| {
125                SdkError::ApiError(SdkApiError {
126                    status,
127                    endpoint: "/com.coralogix.aaa.sso.v2.SamlConfigurationService/SetIdpParameters"
128                        .to_string(),
129                    feature_group: SAML_FEATURE_GROUP_ID.into(),
130                })
131            })
132    }
133
134    /// Sets the active status for a given team.
135    ///
136    /// # Arguments
137    /// * `team_id` - The ID of the team to set the active status for.
138    /// * `is_active` - The active status to set.
139    pub async fn set_active(&self, team_id: u32, is_active: bool) -> Result<()> {
140        let request =
141            make_request_with_metadata(SetActiveRequest { team_id, is_active }, &self.metadata_map);
142        self.service_client
143            .lock()
144            .await
145            .set_active(request)
146            .await
147            .map(|_| ())
148            .map_err(|status| {
149                SdkError::ApiError(SdkApiError {
150                    status,
151                    endpoint: "/com.coralogix.aaa.sso.v2.SamlConfigurationService/SetActive"
152                        .to_string(),
153                    feature_group: SAML_FEATURE_GROUP_ID.into(),
154                })
155            })
156    }
157
158    /// Retrieves the configuration for a given team.
159    ///
160    /// # Arguments
161    /// * `team_id` - The ID of the team to retrieve the configuration for.
162    pub async fn get_configuration(&self, team_id: u32) -> Result<GetConfigurationResponse> {
163        let request =
164            make_request_with_metadata(GetConfigurationRequest { team_id }, &self.metadata_map);
165
166        self.service_client
167            .lock()
168            .await
169            .get_configuration(request)
170            .await
171            .map(|r| r.into_inner())
172            .map_err(|status| {
173                SdkError::ApiError(SdkApiError {
174                    status,
175                    endpoint: "/com.coralogix.aaa.sso.v2.SamlConfigurationService/GetConfiguration"
176                        .to_string(),
177                    feature_group: SAML_FEATURE_GROUP_ID.into(),
178                })
179            })
180    }
181}