Pages

Friday, March 31, 2023

AWS CLI command to find the Quota limit

You could use the AWS CLI to check the current limit for IAM Server certificates by running the AWS CLI command -

$ aws service-quotas get-service-quota --service-code iam --quota-code L-BF35879D --region us-east-1

==============
{
  "Quota": {
    "ServiceCode": "iam",
    "ServiceName": "AWS Identity and Access Management (IAM)",
    "QuotaArn": "arn:aws:servicequotas::895006963947:iam/L-BF35879D",
    "QuotaCode": "L-BF35879D",
    "QuotaName": "Server certificates per account",
    "Value": "55",
    "Unit": "None",
    "Adjustable": "true",
    "GlobalQuota": "true",
    "UsageMetric": null,
    "Period": null,
    "ErrorReason": null
  }
}
==================

Here, you could see the Value as 55.

b. You could use the AWS Console to see this details.

Please make sure to select the region as 'us-east-1' regions.

Then please navigate to AWS services > AWS Identity and Access Management (IAM) > Server certificates per account. 

Here, you could see the list.

Query 2 - Where you could see how many certificates are being used.

Answer  - You could run the following CLI command to list the IAM certificates in your account - 

$ aws iam list-server-certificates

I hope the above information is helpful. In case of any other query or concern, please feel free to update the case and I will be glad to assist you further.

References:

https://docs.aws.amazon.com/cli/latest/reference/service-quotas/get-service-quota.html#examples

[2] https://docs.aws.amazon.com/cli/latest/reference/iam/list-server-certificates.html#examples

Tuesday, March 28, 2023

Delete IAM Certificate using CLI

There are two types of certificates in AWS,
  • ACM Certificate
  • IAM Certificate
to delete IAM Certificate, we can run the below CLI command,

aws iam delete-server-certificate --server-certificate-name <Certificate-name>

to perform this action we need the below permission with Resource's arn
    
    Action:
        "iam:DeleteServerCertificate",
    Resource:
        "arn:aws:iam::*:server-certificate/exst/dev/dev.*",

Friday, February 24, 2023

Receive SQS Messages with AWS Lambda

 To receive the SQS Queue messages from exst-prod-integration-hub-outbound-eit.fifo Queue,

 

  • Expand more on the SQS Queue (exst-prod-integration-hub-outbound-eit.fifo) and check the available messages,
  • Go to Lambda function (PROD_SQS_Receive_Message) and click Add Trigger, Refer below code
  • Select SQS as source,
  • Select exst-prod-integration-hub-outbound-eit.fifo as Queue name,
  • Select 1 as Batch Size then only we’ll get 1 record on CSV file in JSON format,
  • Maximum Concurrency is 2
  • Click Add on the trigger page
  • We can confirm the Lambda on SQS Page under “Lambda Triggers”
  • Once we are done with Lambda Execution, select the Trigger and Delete from SQS Page or else it’ll keep on receive the message and post to Cloud watch event,
  • Go to Cloudwatch and then under Log groups search for “/aws/lambda/PROD_SQS_Receive_Message” and look for Log streams,
  • Under Cloudwatch LogGroup, on top right click “View in Logs Insight”. Click custom timeline and click the date twice so we get log for full day and paste the below code and Run Query.
  • Once the Query is displayed then verify the number of message on the Metrics and click “Export results” and then select “Download table (JSON)”
  • Rename the file with Date and messages and upload to S3 bucket under json folder

 

Alternatively you can follow the manual step.

  • Click the Log Stream and click ACTIONS and select “Download search results (CSV)” then we can export it to csv format
  • Login to https://jsonlint.com/ and validate the JSON and send to Laurie

 

 

SQS Queue : exst-prod-integration-hub-outbound-eit.fifo

Lambda Function: PROD_SQS_Receive_Message

 

import json

def lambda_handler(event, context):

                # TODO implement

                for x in event['Records']:

               print(x)

 

Cloudwatch : /aws/lambda/PROD_SQS_Receive_Message

fields @message

| filter @message like "messageId"

 

S3 Bucket URL : exst-prod-ih-outbound-eit-sqs

  1. Amazon S3
  2. Buckets
  3. exst-prod-ih-outbound-eit-sqs
  4. json/

 

Saturday, January 21, 2023

Git Push & Pull Submodules

# To pull the Git branch along with submodules 

git clone --recurse-submodules <Repo URL>

for eg) 

git clone --recurse-submodules StruxureWareCloud@URL/StruxureWareCloud/Exchange/exst_infra


# To change / switch the branch

git branch

git checkout <new branch>


# To branch out to new branch

git remote -v

git checkout -b Feature/Blah_Blah develop


# To push to Git Branch

git push --set-upstream origin Feature/Listener_rule_frontend


git push -u origin

Terraform State Removal

 Incase if we want to remove the operation from state file please follow these steps.

# To Check the Docker Process running and if found then grab the process ID

docker ps 


#To Login to Docker container using bash shell

docker exec -it <Docker PS ID> /bin/bash


cd DIRECTORY

# To Initiate the Terraform
terraform init

# To list the workspaces
terraform workspace list

# To Connect to the Workspace
terraform workspace select <Workspace NAME>

# To List the state files / operations
terraform state list

# To Remove the step from the State file
terraform state rm aws_lb_listener.frontend_listener_https[0]

PEM Certificate description & Import

 While we try to upload the PEM Certificate to ACM or Listener Certificate for LB

We need to upload the Private Key and Certificate Body to the Certificate manager


We can easily identify the Private Key from the PEM file whereas for Certificate Body we need to use PTI-Intermediate Certificate.


# Private key of the SSL certificate
-----BEGIN PRIVATE KEY-----
Blah Blah Blah

-----END PRIVATE KEY-----

# Public key of the SSL certificate (the server certificate)
-----BEGIN CERTIFICATE-----
Blah Blah Blah
-----END CERTIFICATE----- # Trust chain intermediate certificate -----BEGIN CERTIFICATE-----
Blah Blah Blah
-----END CERTIFICATE----- # Trust chain root certificate -----BEGIN CERTIFICATE-----
Blah Blah Blah
-----END CERTIFICATE-----


We can extract the private key form a PFX to a PEM file with this command:
# openssl pkcs12 -in filename.pfx -nocerts -out key.pem

Exporting the certificate only:
# openssl pkcs12 -in filename.pfx -clcerts -nokeys -out cert.pem

Removing the password from the extracted private key:
# openssl rsa -in key.pem -out server.key

Import (AWS CLI) - [Credits to AWS Doc]

The following example shows how to import a certificate using the AWS Command Line Interface (AWS CLI). The example assumes the following:

  • The PEM-encoded certificate is stored in a file named Certificate.pem.

  • The PEM-encoded certificate chain is stored in a file named CertificateChain.pem.

  • The PEM-encoded, unencrypted private key is stored in a file named PrivateKey.pem.

To use the following example, replace the file names with your own and type the command on one continuous line. The following example includes line breaks and extra spaces to make it easier to read.

$ aws acm import-certificate --certificate fileb://Certificate.pem \ --certificate-chain fileb://CertificateChain.pem \ --private-key fileb://PrivateKey.pem


# To check the contents of the pfx file

openssl pkcs12 -info -in <PFX File>

WORKING SOLUTION :

To extract the certificates in PEM format from a PFX file, follow the below procedure:

  1. Copy the PFX file to a Linux system with the OpenSSL utility installed.
  2. Log on to that Linux system and navigate to the directory where the PFX file was copied in the previous step.
  3. Execute the below commands to extract the certificates:
    1. Extract the RSA Key from the PFX file:
      $ openssl pkcs12 -in <PFX_file> -nocerts -nodes -out nutanix-key-pfx.pem
    2. Extract the Public Certificate from the PFX file:
      $ openssl pkcs12 -in <PFX_file> -clcerts -nokeys -out nutanix-cert-pfx.pem
    3. Extract the CA Chain from the PFX file:
      $ openssl pkcs12 -in <PFX_file> -cacerts -nokeys -chain -out ca-pfx.pem
  4. Convert the RSA Key from PFX format to PEM:
    $ openssl rsa -in nutanix-key-pfx.pem -out nutanix-key.pem
  5. Convert the x509 Public Certificate and CA Chain from PFX to PEM format:
    $ openssl x509 -in nutanix-cert-pfx.pem -out nutanix-cert.pem
    $ openssl x509 -in ca-pfx.pem -out ca.pem
  6. Download nutanix-key.pem, nutanix-cert.pem and ca.pem from the Linux system to a local desktop.
  7. After following the steps above, the needed certificates and keys will be generated in preset working directory and it can be used as follows.
    • Private Key Type: RSA 2048-bit
    • Private Key: nutanix-key.pem
    • Public Certificate: nutanix-cert.pem
    • CA Certificate/Chain: ca.pem

openssl pkcs12 -in dsc-shop-uat.se.com.pfx -nocerts -nodes -out key-pfx.pem

openssl pkcs12 -in dsc-shop-uat.se.com.pfx -clcerts -nokeys -out cert-pfx.pem

openssl pkcs12 -in dsc-shop-uat.se.com.pfx -cacerts -nokeys -chain -out ca-pfx.pem

openssl rsa -in key-pfx.pem -out key.pem

openssl x509 -in cert-pfx.pem -out uat-wildcard.pem

openssl x509 -in ca-pfx.pem -out chain.pem

cat UAT\ Chain/UAT-SUBCA-01.cer > cert-chain.pem

cat chain.pem >> cert-chain.pem

cat cert-chain.pem



While import the Certificate: (Select IAM as import to)

Paste key.pem for Certificate private key (PEM encoded)

Paste uat-wildcard.pem to Certificate body (PEM encoded)

Paste cert-chain.pem to the Certificate chain (PEM encoded) - optional


##################

To Find the Expiry date for the certificate

##################

openssl x509 -in uat-wildcard.pem -noout -enddate

Incase if you face the below issue while try to extract

Error outputting keys and certificates

00DE31EC01000000:error:0308010C:digital envelope routines:inner_evp_generic_fetch:unsupported:crypto/evp/evp_fetch.c:341:Global default library context, Algorithm (RC2-40-CBC : 0), Properties ()

Please use legacy as on option,

$ openssl pkcs12 -in dsc-shop-uat.se.com.pfx -nocerts -nodes -legacy -out key-pfx.pem
$ openssl pkcs12 -in dsc-shop-uat.se.com.pfx -clcerts -nokeys -legacy -out cert-pfx.pem

Wednesday, May 12, 2021

Enable IP Forwarding for Docker

 By default even if we are able to communicate to outside world from linux, the communication will not happen from docker.


Add the following to /etc/sysctl.conf:

net.ipv4.ip_forward=1

then restart the network service.

sudo service network restart