Skip to content

Commit

Permalink
Add Org level API key
Browse files Browse the repository at this point in the history
  • Loading branch information
assafad1 committed Dec 2, 2024
1 parent d93b1d5 commit 0dff3cf
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 68 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ The SDK can be configured using environment variables:

- `CORALOGIX_TEAM_API_KEY`: The API key that is used for all team-level interactions. Note that it has to have appropriate permissions. Read the [docs](https://coralogix.com/docs/api-keys/) for more information.
- `CORALOGIX_USER_API_KEY`: The API key that is used for all user-level interactions. Note that it has to have appropriate permissions. Read the [docs](https://coralogix.com/docs/api-keys/) for more information.
- `CORALOGIX_ORG_API_KEY`: The API key that is used for all organization-level interactions.
- `CORALGOIX_REGION`: The region/cluster to connect to as a shorthand (EU2, AP1, etc. read more [here](https://coralogix.com/docs/coralogix-domain/)).

Furthermore, if you want to run the examples locally, you're going to to have set the following environment variables:
Expand Down
18 changes: 18 additions & 0 deletions go/callPropertiesCreator.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type CallPropertiesCreator struct {
coraglogixRegion string
teamsLevelAPIKey string
userLevelAPIKey string
orgLevelAPIKey string
correlationID string
sdkVersion string
//allowRetry bool
Expand Down Expand Up @@ -75,6 +76,21 @@ func (c CallPropertiesCreator) GetUserLevelCallProperties(ctx context.Context) (
return &CallProperties{Ctx: ctx, Connection: conn, CallOptions: callOptions}, nil
}

// GetOrgLevelCallProperties returns a new CallProperties object built from an organization-level API key. It essentially prepares the context, connection, and call options for a gRPC call.
func (c CallPropertiesCreator) GetOrgLevelCallProperties(ctx context.Context) (*CallProperties, error) {
ctx = createContext(ctx, c.orgLevelAPIKey, c.correlationID, c.sdkVersion)

endpoint := CoralogixGrpcEndpointFromRegion(c.coraglogixRegion)
conn, err := createSecureConnection(endpoint)
if err != nil {
return nil, err
}

callOptions := createCallOptions()

return &CallProperties{Ctx: ctx, Connection: conn, CallOptions: callOptions}, nil
}

func createCallOptions() []grpc.CallOption {
var callOptions []grpc.CallOption
callOptions = append(callOptions, grpc_retry.WithMax(5))
Expand All @@ -99,6 +115,7 @@ func NewCallPropertiesCreator(region string, authContext AuthContext) *CallPrope
coraglogixRegion: region,
teamsLevelAPIKey: authContext.teamLevelAPIKey,
userLevelAPIKey: authContext.userLevelAPIKey,
orgLevelAPIKey: authContext.orgLevelAPIKey,
correlationID: uuid.New().String(),
sdkVersion: vanillaSdkVersion,
}
Expand All @@ -109,6 +126,7 @@ func NewCallPropertiesCreatorTerraformOperator(region string, authContext AuthCo
return &CallPropertiesCreator{
coraglogixRegion: region,
teamsLevelAPIKey: authContext.teamLevelAPIKey,
orgLevelAPIKey: authContext.orgLevelAPIKey,
userLevelAPIKey: authContext.userLevelAPIKey,
correlationID: uuid.New().String(),
sdkVersion: terraformOperatorVersion,
Expand Down
47 changes: 19 additions & 28 deletions go/cxsdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,8 @@ func (c *ClientSet) DataUsage() *DataUsageClient {
}

// NewClientSet Creates a new ClientSet.
func NewClientSet(targetURL, teamsLevelAPIKey string, userLevelAPIKey string) *ClientSet {
authContext := NewAuthContext(teamsLevelAPIKey, userLevelAPIKey)
func NewClientSet(targetURL, teamsLevelAPIKey, userLevelAPIKey, orgLevelAPIKey string) *ClientSet {
authContext := NewAuthContext(teamsLevelAPIKey, userLevelAPIKey, orgLevelAPIKey)
apikeyCPC := NewCallPropertiesCreator(targetURL, authContext)

return &ClientSet{
Expand Down Expand Up @@ -196,6 +196,9 @@ const EnvCoralogixTeamLevelAPIKey = "CORALOGIX_TEAM_API_KEY"
// EnvCoralogixUserLevelAPIKey is the name of the environment variable that contains the Coralogix User API key.
const EnvCoralogixUserLevelAPIKey = "CORALOGIX_USER_API_KEY"

// EnvCoralogixOrgLevelAPIKey is the name of the environment variable that contains the Coralogix Organization API key.
const EnvCoralogixOrgLevelAPIKey = "CORALOGIX_ORG_API_KEY"

// CoralogixRegionFromEnv reads the Coralogix region from environment variables.
func CoralogixRegionFromEnv() (string, error) {
regionIdentifier := strings.ToLower(os.Getenv(EnvCoralogxRegion))
Expand Down Expand Up @@ -271,56 +274,44 @@ const (
type AuthContext struct {
teamLevelAPIKey string
userLevelAPIKey string
orgLevelAPIKey string
}

// NewAuthContext creates a new AuthContext.
func NewAuthContext(teamLevelAPIKey, userLevelAPIKey string) AuthContext {
func NewAuthContext(teamLevelAPIKey, userLevelAPIKey, orgLevelAPIKey string) AuthContext {
if teamLevelAPIKey == "" {
log.Println("Warning: teamLevelAPIKey was not provided. Some functionality will not be available.")
}
if userLevelAPIKey == "" {
log.Println("Warning: userLevelAPIKey was not provided. Some functionality will not be available.")
}
if orgLevelAPIKey == "" {
log.Println("Warning: orgLevelAPIKey was not provided. Some functionality will not be available.")
}
return AuthContext{
teamLevelAPIKey: teamLevelAPIKey,
userLevelAPIKey: userLevelAPIKey,
orgLevelAPIKey: orgLevelAPIKey,
}
}

// AuthContextFromEnv reads the Coralogix API keys from environment variables.
func AuthContextFromEnv() (AuthContext, error) {
teamLevelAPIKey, err := CoralogixTeamsLevelAPIKeyFromEnv()
if err != nil {
return AuthContext{}, err
}
userLevelAPIKey, err := CoralogixUserLevelAPIKeyFromEnv()
if err != nil {
return AuthContext{}, err
teamLevelAPIKey := os.Getenv(EnvCoralogixTeamLevelAPIKey)
userLevelAPIKey := os.Getenv(EnvCoralogixUserLevelAPIKey)
orgLevelAPIKey := os.Getenv(EnvCoralogixOrgLevelAPIKey)
if teamLevelAPIKey == "" && userLevelAPIKey == "" && orgLevelAPIKey == "" {
return AuthContext{}, fmt.Errorf("at least one of %s, %s, or %s must be set", EnvCoralogixTeamLevelAPIKey, EnvCoralogixUserLevelAPIKey, EnvCoralogixOrgLevelAPIKey)
}
if teamLevelAPIKey == "" {
log.Println("Warning: teamLevelAPIKey is empty. Some functionality will not be available.")
}
if userLevelAPIKey == "" {
log.Println("Warning: userLevelAPIKey is empty. Some functionality will not be available.")
}

return AuthContext{teamLevelAPIKey: teamLevelAPIKey, userLevelAPIKey: userLevelAPIKey}, nil
}

// CoralogixTeamsLevelAPIKeyFromEnv reads the Coralogix Team API key from environment variables.
func CoralogixTeamsLevelAPIKeyFromEnv() (string, error) {
apiKey := os.Getenv(EnvCoralogixTeamLevelAPIKey)
if apiKey == "" {
return "", fmt.Errorf("the %s environment variable is not set", EnvCoralogixTeamLevelAPIKey)
if orgLevelAPIKey == "" {
log.Println("Warning: orgLevelAPIKey is empty. Some functionality will not be available.")
}
return apiKey, nil
}

// CoralogixUserLevelAPIKeyFromEnv reads the Coralogix User API key from environment variables.
func CoralogixUserLevelAPIKeyFromEnv() (string, error) {
apiKey := os.Getenv(EnvCoralogixUserLevelAPIKey)
if apiKey == "" {
return "", fmt.Errorf("the %s environment variable is not set", EnvCoralogixUserLevelAPIKey)
}
return apiKey, nil
return AuthContext{teamLevelAPIKey: teamLevelAPIKey, userLevelAPIKey: userLevelAPIKey, orgLevelAPIKey: orgLevelAPIKey}, nil
}
14 changes: 7 additions & 7 deletions go/teams-client.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ type TeamsClient struct {

// Create creates a new team.
func (c TeamsClient) Create(ctx context.Context, req *CreateTeamInOrgRequest) (*teams.CreateTeamInOrgResponse, error) {
callProperties, err := c.callPropertiesCreator.GetTeamsLevelCallProperties(ctx)
callProperties, err := c.callPropertiesCreator.GetOrgLevelCallProperties(ctx)
if err != nil {
return nil, err
}
Expand All @@ -77,7 +77,7 @@ func (c TeamsClient) Create(ctx context.Context, req *CreateTeamInOrgRequest) (*

// Update updates a team.
func (c TeamsClient) Update(ctx context.Context, req *UpdateTeamRequest) (*teams.UpdateTeamResponse, error) {
callProperties, err := c.callPropertiesCreator.GetTeamsLevelCallProperties(ctx)
callProperties, err := c.callPropertiesCreator.GetOrgLevelCallProperties(ctx)
if err != nil {
return nil, err
}
Expand All @@ -91,7 +91,7 @@ func (c TeamsClient) Update(ctx context.Context, req *UpdateTeamRequest) (*teams

// Get gets a team.
func (c TeamsClient) Get(ctx context.Context, req *GetTeamRequest) (*teams.GetTeamResponse, error) {
callProperties, err := c.callPropertiesCreator.GetTeamsLevelCallProperties(ctx)
callProperties, err := c.callPropertiesCreator.GetOrgLevelCallProperties(ctx)
if err != nil {
return nil, err
}
Expand All @@ -105,7 +105,7 @@ func (c TeamsClient) Get(ctx context.Context, req *GetTeamRequest) (*teams.GetTe

// Delete deletes a team.
func (c TeamsClient) Delete(ctx context.Context, req *DeleteTeamRequest) (*teams.DeleteTeamResponse, error) {
callProperties, err := c.callPropertiesCreator.GetTeamsLevelCallProperties(ctx)
callProperties, err := c.callPropertiesCreator.GetOrgLevelCallProperties(ctx)
if err != nil {
return nil, err
}
Expand All @@ -119,7 +119,7 @@ func (c TeamsClient) Delete(ctx context.Context, req *DeleteTeamRequest) (*teams

// SetDailyQuota sets the daily quota for a team.
func (c TeamsClient) SetDailyQuota(ctx context.Context, req *SetDailyQuotaRequest) (*teams.SetDailyQuotaResponse, error) {
callProperties, err := c.callPropertiesCreator.GetTeamsLevelCallProperties(ctx)
callProperties, err := c.callPropertiesCreator.GetOrgLevelCallProperties(ctx)
if err != nil {
return nil, err
}
Expand All @@ -133,7 +133,7 @@ func (c TeamsClient) SetDailyQuota(ctx context.Context, req *SetDailyQuotaReques

// GetQuota gets the quota for a team.
func (c TeamsClient) GetQuota(ctx context.Context, req *GetTeamQuotaRequest) (*teams.GetTeamQuotaResponse, error) {
callProperties, err := c.callPropertiesCreator.GetTeamsLevelCallProperties(ctx)
callProperties, err := c.callPropertiesCreator.GetOrgLevelCallProperties(ctx)
if err != nil {
return nil, err
}
Expand All @@ -147,7 +147,7 @@ func (c TeamsClient) GetQuota(ctx context.Context, req *GetTeamQuotaRequest) (*t

// MoveQuota moves the quota from one team to another.
func (c TeamsClient) MoveQuota(ctx context.Context, req *MoveQuotaRequest) (*teams.MoveQuotaResponse, error) {
callProperties, err := c.callPropertiesCreator.GetTeamsLevelCallProperties(ctx)
callProperties, err := c.callPropertiesCreator.GetOrgLevelCallProperties(ctx)
if err != nil {
return nil, err
}
Expand Down
29 changes: 24 additions & 5 deletions rust/cx-sdk/src/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,11 @@
// limitations under the License.

use crate::error::Result;
use std::fmt::{
Debug,
Formatter,
};
use std::fmt::{Debug, Formatter};

const ENV_TEAM_API_KEY: &str = "CORALOGIX_TEAM_API_KEY";
const ENV_USER_API_KEY: &str = "CORALOGIX_USER_API_KEY";
const ENV_ORG_API_KEY: &str = "CORALOGIX_ORG_API_KEY";

#[derive(Clone, Hash, PartialEq, Eq, PartialOrd, Ord, Default)]
/// The authentication context.
Expand All @@ -28,6 +26,8 @@ pub struct AuthContext {
pub(crate) team_level_api_key: ApiKey,
/// The user level API key.
pub(crate) user_level_api_key: ApiKey,
/// The organization level API key.
pub(crate) org_level_api_key: ApiKey,
}

impl AuthContext {
Expand All @@ -36,16 +36,27 @@ impl AuthContext {
/// # Arguments
/// * `team_level_api_key` - The team level API key.
/// * `user_level_api_key` - The user level API key.
pub fn new(team_level_api_key: Option<ApiKey>, user_level_api_key: Option<ApiKey>) -> Self {
/// * `org_level_api_key` - The organization level API key.
pub fn new(
team_level_api_key: Option<ApiKey>,
user_level_api_key: Option<ApiKey>,
org_level_api_key: Option<ApiKey>,
) -> Self {
if team_level_api_key.is_none() {
log::warn!("Team level API key is not set. some functionality may not be available.");
}
if user_level_api_key.is_none() {
log::warn!("User level API key is not set. some functionality may not be available.");
}
if org_level_api_key.is_none() {
log::warn!(
"Organization level API key is not set. some functionality may not be available."
);
}
AuthContext {
team_level_api_key: team_level_api_key.unwrap_or_default(),
user_level_api_key: user_level_api_key.unwrap_or_default(),
org_level_api_key: org_level_api_key.unwrap_or_default(),
}
}

Expand All @@ -54,6 +65,7 @@ impl AuthContext {
AuthContext::new(
ApiKey::teams_level_from_env().ok(),
ApiKey::user_level_from_env().ok(),
ApiKey::org_level_from_env().ok(),
)
}
}
Expand Down Expand Up @@ -81,6 +93,13 @@ impl ApiKey {
.map(ApiKey)
.map_err(From::from)
}

/// Creates a new organization-level API key from the ENV_API_KEY environment variable.
pub fn org_level_from_env() -> Result<Self> {
std::env::var(ENV_ORG_API_KEY)
.map(ApiKey)
.map_err(From::from)
}
}

impl From<&str> for ApiKey {
Expand Down
35 changes: 7 additions & 28 deletions rust/cx-sdk/src/client/teams.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,43 +16,22 @@ use std::str::FromStr;

use crate::{
auth::AuthContext,
error::{
Result,
SdkApiError,
SdkError,
},
error::{Result, SdkApiError, SdkError},
metadata::CallProperties,
util::make_request_with_metadata,
};

use cx_api::proto::com::coralogixapis::aaa::organisations::v2::{
CreateTeamInOrgRequest,
CreateTeamInOrgResponse,
DeleteTeamRequest,
DeleteTeamResponse,
GetTeamQuotaRequest,
GetTeamQuotaResponse,
GetTeamRequest,
GetTeamResponse,
ListTeamsRequest,
ListTeamsResponse,
MoveQuotaRequest,
MoveQuotaResponse,
SetDailyQuotaRequest,
SetDailyQuotaResponse,
TeamId,
UpdateTeamRequest,
team_service_client::TeamServiceClient, CreateTeamInOrgRequest, CreateTeamInOrgResponse,
DeleteTeamRequest, DeleteTeamResponse, GetTeamQuotaRequest, GetTeamQuotaResponse,
GetTeamRequest, GetTeamResponse, ListTeamsRequest, ListTeamsResponse, MoveQuotaRequest,
MoveQuotaResponse, SetDailyQuotaRequest, SetDailyQuotaResponse, TeamId, UpdateTeamRequest,
UpdateTeamResponse,
team_service_client::TeamServiceClient,
};
use tokio::sync::Mutex;
use tonic::{
metadata::MetadataMap,
transport::{
Channel,
ClientTlsConfig,
Endpoint,
},
transport::{Channel, ClientTlsConfig, Endpoint},
};

use crate::CoralogixRegion;
Expand All @@ -74,7 +53,7 @@ impl TeamsClient {
let channel: Channel = Endpoint::from_str(region.grpc_endpoint().as_str())?
.tls_config(ClientTlsConfig::new().with_native_roots())?
.connect_lazy();
let request_metadata: CallProperties = (&auth_context.team_level_api_key).into();
let request_metadata: CallProperties = (&auth_context.org_level_api_key).into();
Ok(Self {
metadata_map: request_metadata.to_metadata_map(),
service_client: Mutex::new(TeamServiceClient::new(channel)),
Expand Down

0 comments on commit 0dff3cf

Please sign in to comment.