Sunday, 14 June 2020

PKI Certificate

PKI Uses

Understood Nothing

You've probably heard the term PKI being used in a work environment. And if you're into IT security, you'll be well familiar with the term. PKI stands for Public Key Infrastructure. Essentially, what it is, if we can water it down to a single sentence, is it's a hierarchy of digital security certificates, where unique public and private key pairs are issued for each certificate. So certificate authorities, otherwise called CAs, are authorities that issue certificates. They can renew certificates because certificates have an expiration date, and they could be renewed before they expire. They also do have an expiration date where they will simply expire by themselves, where they can no longer be used.

And also certificate authorities are responsible for revoking certificates, maybe due to compromise of a station or because a user is leaving the organization. We also have the option of working with our own private internal certificate authority. You might install a product like Microsoft Active Directory Certificate Services or maybe you would use OpenSSL. But either way you can have a private CA where you sign your own certificates. The only issue with that is that devices out there on the network don't trust your private, self-created certificate authority.

So you would have to install the root certificate for your CA on those devices. Of course, we've got public CAs where we could have a certificate signing request that we've generated issued up to the public CA. And after they've done their due diligence, their CA can digitally sign and return our certificate. We might do that, for example, if we need a website certificate that's trusted globally. So what is PKI used for? Well, it uses a public key. And the public key can be shared publicly with any user or device, that's why it's called a public key.

So the recipient public key then would be used when we encrypt a message that we're sending to that recipient. We need their public key to encrypt it. Public keys are also used to verify digital signatures. Now the private key must be available only to the key owner, whether that's a device or whether it's a user. So it's not like a public key. Private keys are going to be kept private only to the owner, and they can also be embedded in things like cards, like smartcards. So encrypted messages then are decrypted using this key.

Remember up above you need a public key to encrypt the message to somebody. You need their public key. Well, they need their mathematically related private key from the key pair to decrypt that message. The private key is also used to create a digital signature. So if we have a need, let's say, of e-mail confidentiality, how does PKI serve this need? Well, we can encrypt the message with the recipient's public key. The second item here is what if we need e-mail authenticity and integrity? Well, a solution is to generate a message hash and encrypt that hash with the sender's private key.

And often, that's done seamlessly and transparently in modern e-mail programs. What if we have a need to secure network communication to a web server? Well, we would use a PKI certificate issued to that web server and make sure that TLS version 1.1 or higher are used. And then we would enable HTTPS on the web server. Now TLS version 1.1 is a secure way to secure network communications using a PKI certificate.

Using the older SSL security protocol should never be done due to its large number of vulnerabilities. And there are even vulnerabilities associated with TLS v1.0. And that's why we're saying here you should start at TLS version 1.1. What if we have a need to use Multi-Factor Authentication to a VPN? Well, PKI could solve that because we might embed a private key on a smartcard, which would be used. The smartcard would have to be available and presented when we need to authenticate the VPN.

Finally, what if we have a need to have a single card, a physical card that people carry with them, that would allow things like facility access, as well as computer access? Well, that's called a Common Access Card or a CAC. Essentially, it's a smartcard that has multiple uses that contains an embedded private key.

 

PKI Hierarchy

Understood Nothing

The PKI hierarchy is a hierarchy of digital security certificates, where certificates are issued and also managed after they're issued by a certificate authority. We could have a private certificate authority or a private CA that we set up within our own organization and that private CA issues certificates and manages them. The only thing we would have to consider is on all devices that need to trust certificates issued by that private CA, we would have to install that private CA's root certificate.

Public CAs are available for a fee as well. Where we could send a certificate signing request up to the public CA, either through a web form or through an e-mail file attachment. And after the public certificate authority does their due diligence to ensure that the information that we have provided in the request is valid, they can use their CA, private key to digitally sign our certificate.

And if it's a public CA, it will be trusted globally. So we might do this for example, if we have a public website that we want to secure using HTTPS.

The PKI hierarchy starts at the very top with the root certificate authority or root CA. Now from there, the root CA could issue certificates directly, but in a larger enterprise, you would have subordinate CAs.

Maybe for different parts of the world, different countries, different provinces, different regions. Or even within a large corporation, we might have different CAs for different departments, or different projects. And each of those subordinate CAs, in turn, can issue its own certificates.

A diagram explaining the PKI hierarchy is displayed. The Root CA is at the top. The Root CA is connected to Subordinate CA1 and Subordinate CA2. Subordinate CA1 and Subordinate CA2 are connected to Certificates. 

Now if that's the case, then the root CA doesn't need to be there, doesn't need to be online and available all the time, unless you're going to be doing something like creating a new subordinate CA. So the issuing certificate authority digitally signs issued certificates. And as we were saying, the root CA really, from a security perspective, should be brought offline when it's not being used.

The reason is because if the root CA is always left online and by chance, it gets compromised. Now it has a higher degree of being compromised if it's always online. If that root CA at the top of the hierarchy is compromised, all subordinate certificates are compromised, including subordinate certificate authorities. So you can see why this is a big deal.

Now, if we have a compromised subordinate CA, then certificates issued by that CA are compromised, nothing above it in the hierarchy. So it's always important then that we consider the PKI hierarchy when we're planning our public key infrastructure, or if we're making changes to an existing configuration and trying to harden it.

 

Create a PKI Certificate Authority Using Linux

In this demonstration, I'm going to use Linux to create a PKI Certificate Authority. The first thing you're going to need to do is to make sure you have the appropriate module or tools installed to work with PKI in Linux. So for example, I could use the apt-get install command if I wanted to install the openssl package. Here I can see that openssl is already installed with the newest version.

He opens the root@kali: / window on the Linux operating system. The root@kali: /# prompt is displayed. Then he executes the following command: apt-get install openssl. The output displays the status of the latest version of the openssl installed. 

So now what I'm going to do is make a directory here on the root of the file system, and I'm going to call it pki. And I'm going to change directory into the pki directory. Because any files that are generated, I want organized in this location.

So what I want to do then is use openssl, this is the command that's built-in once you've installed this. And I got to tell it I want to generate some RSA keys, rsa, I want to use aes256 bit keys. So AES is the algorithm. And I have to give it the name for an output file. So I'm going to call this let's say ca_priv for private.key. That's the private key file that will be generated. And then we're going to tell it that at the RSA level, we want to make sure that we use 2048 bits. And we going to go ahead and press Enter.

 

Generating CA Private Key:

He executes the following command: openssl genrsa -aes256 -out ca_priv.key 2048. The output displays a message: Generating RSA private key, 2048 bit long modulus, and asks to enter a pass phrase for ca_priv.key.

Okay, so we now have a private key that's 2048 bits, that's good. So the next thing we're going to do is enter a pass phrase for the private key. I'm going to put in a password, and then I will verify that same password.

Now the next thing that we want to do is actually generate a CA or certificate authority certificate. So to do that I'm going to run openssl req for request -new and then -x509. x509 is the standard that's used for PKI certificates. I'm going to specify my private key with -key. So that was called ca_priv.key. Then I'm going to specify the signature algorithm, because remember a certificate authority digitally signs certificates.

Here it's going to use sha256. I'm going to specify in days, the lifetime of this certificate, so it's going to be let's say, only 365 days, that's not very long for a CA. Commonly it might be five years, maybe even ten years, but here I'm just going to make it one year. And I'm going to specify the name of the output file for this CA certificate. So -out and I'm going to call it ca, let's say .pem, standard file format.

He executes the following command: openssl req -new -x509 -key ca_priv.key -sha256 -days 365 -out ca.pem. The output prompts to Enter the pass phrase for ca_priv.key. 

Now because we referenced our private key it wants the pass phrase for it, that's good. You always want a pass phrase on a private key file. So I'll go ahead and enter that, and then I have to fill in some details because we're establishing a certificate authority, so it has questions. So the country name, two letter code, in this case, for Canada, I'll put in CA.

He enters the pass phrase and presses the Enter key. The output includes a message that reads, You are about to be asked to enter information that will be incorporated into your certificate request. Country Name (2 letter code) [AU]:. 

 

 State or province, I'll fill in the appropriate value, same with the locality, that's city name. Locality is filled in, the organization name, I'll just put in here as FakeCo and organizational unit, maybe Hq, headquarters. And a common name, so this would be for my certificate authority, I'm going to call it MyCA. And then an email address for a user. So that would be an administrative account, for example, so I'll fill that in.

He enters the email address as user@fakeco.com. He again presses the Enter key and the prompt remains the same. 

ca_priv.key -> Private Key

ca.pem -> Certificate Authority

 

Web of Trust

When you acquire a passport or a driver's license, that is trusted by other government departments, and agencies, and institutions, because they trust the issuer of the passport or the driver's license. And with Public Key Infrastructure or PKI, the same type of web of trust exists.

So in the PKI hierarchy, the issuing certificate authority or CA digitally signs issued certificates using its private key. Now, only the CA owner has access to that private key and the signatures created from it. Now, the signature is then used to establish trust, because if we trust at the certificate authority then by extension, we also trust any certificates that have that certificate authority digital signature. Pictured on the screen, we've got a screenshot on the right of the trusted root's certification authority's store on a Windows machine.

The screenshot of Trusted Root Certification Authorities displays a table which includes Issued To and Issued By column headers. 

 

 In other words, different types of operating systems, in this case, Windows, have a list of public certification authorities who they trust. And if they trust that public certification authority, by extension, they trust any certificates issued by that authority. So devices then, like your laptop, your desktop, your smartphone, needs to trust certificate signers. Now, we're looking at the Windows trusted certificate store, that's what we were talking about. But the same type of system exists on a Linux host using the Linux CA certs key store.

So certificate trust stores then have a list of trusted root and subordinate certificate authorities. And, as we know, if you trust the certificate authority, then you also trust all the certificates issued by that certificate authority. That's what the web of trust is about. Now, with private certificate authorities that you might create within your own organization, as opposed to public certificate authorities. Certificate trust stores on devices will not automatically trust the CA. They won't contain the certificate, the root certificate, and so they will not trust any issued certificates.

So the trusted root certificate then you would simply have to import on devices in your organization. Now, the private CA will issue certificates and remember that they will then be trusted on devices. The trust is established based on the CA signature contained within issued certificates. So again, the overall theme is very simple. If you trust the certificate authority, then you trust all of these certificates that are issued by that authority. Just as in the same case, if you trust an issuer of passports within a country, then you will trust all of the passports issued by that country.

 

PKI Certificates

With Public Key Infrastructure, PKI certificates are issued by a certificate authority, by a CA. And these certificates could be issued to a user. Or may have a certificate issued to a device like a smartphone that needs to authenticate to a VPN. Or we can even have certificates issued to be used by software components or services. So these certificates are issued by the CA for security purposes such as to encrypt files or encrypt network communications.

To verify file integrity or message integrity over a network to ensure it's not been changed. And also for authentication such as over the network with messages so we can trust that the message came from the sender it says it came from. So we can store PKI certificates in many different places. It might be stored in a file, in the file system on a device. A PKI certificate might be stored on a smartcard. So there are many different ways that we can store these PKI certificates.

As a matter of fact, if you're using a business computer like a laptop, you might even have the trusted platform module TPM firmware chip which can also incidentally store PKI certificate information. So what's in a PKI certificate anyway? How does this work to allow security in an environment? Well, we know the PKI certificate is issued by a CA and that it can be stored in a variety of different ways. What's in it?

Well, the version of the X.509 standard that's used to generate that PKI certificate. The digital signature of the certificate authority, which is important to establish trust. The signature algorithm that was used by the certificate authority to create the signature. For example, maybe it was SHA256. The certificate serial number is certainly stored within the certificate.

The date the certificate was issued, when it expires, how the certificate and the keys can be used whether only for encryption, or maybe only for authentication, or both. The subject name which is important. The subject name could be the URL of a website, which has to match what people are typing in for the certificate to be trusted. Or maybe the subject name is a user e-mail address, if we're using user certificates to encrypt and digitally sign e-mail communications. So the certificate can also contain public and, in some cases, private keys.

Now the purpose here would be for encryption and decryption. For example, we would encrypt with the recipient's public key if we sent something to them. And they decrypt with the related private key. Digital signatures are created with the private key and verified with the public. In some cases, the private key will not be stored within a certificate. So it really depends on the environment and how the certificates are being used.

PKI certificates can also be issued from templates depending on the solution that you're using. And as you would guess, the template is just a blueprint of all of the details that will be placed within the PKI certificate. The items that we just talked about. Certificates and public or private keys can also be exported.

Now if we think about this, we might have a certificate that contains a private key. And we might want to backup the private key in a safe place. So we can actually export it as long as when that certificate was issued, exportation of the private key was marked as being allowed. Now public keys can be shared with anybody. And so it's okay to provide a public key to anyone over the internet, whether you trust them already or not, however, not the private key. And as we said, you might export the private key from a certificate for backup purposes. Private keys should only be available to the entity to which it was issued, be that a piece of software, or user, or a device.

Now in the PKI certificate, we mentioned the subject name field. And we said that this must match the entity such as the URL of the website or the e-mail address of the user. So if that's not the case, then we won't have trust established. So in the case of a secured website, if the URL that people are typing in doesn't match the URL in the subject name field in the certificate, then depending on the web browser people are using will determine exactly what message pops up. But in essence, that site will not be trusted. And that is not a good thing.

 

Issue a PKI Certificate Using Linux

In this demonstration, I will use OpenSSL in Linux to issue a PKI certificate to be used by a Linux web server.

The first time here, we're going to use it to generate a private key file that will be used by our web server certificate. And to do that, I'll run openssl genrsa, I want to generate an RSA type of key, that's the algorithm that will be used. And the output file will be, let's say, the file name can be anything, but I'm going to say www.site.com.priv.key. So I know it's the private key and I want it to be a 2048 bit RSA key.

He executes the following command: openssl genrsa -out www.site.com.priv.key 2048. The output displays a message: Generating RSA private key, 2048 bit long modulus. 

 

Now, I have to generate a certificate signing request. What I'm doing is preparing a file here that needs to be submitted to the certificate authority whether it's on this host or whether we send it through e-mail or paste it in to a web form field. One way or another, the certificate signing request has to be sent to the CA because we need the CA to digitally sign it and send it back to us. So let's generate a certificate signing request, openssl req -new.

And I have to tell it to the key -key, the private key that is, that I've generated previously for this web server certificate. And then I have to specify an output file for this certificate signing request. So how about we call it www.site.com.csr, for certificate signing request.

He executes the following command: openssl req -new -key www.site.com.priv.key -out www.site.com.csr. The output includes a message that reads, You are about to be asked to enter information that will be incorporated into your certificate request and the field: Country Name (2 letter code) [AU]. 

It's going to ask me for a bunch of stuff like the Country name and so on. I'm going to specify, let's say, CA two character country code, state or a province of some kind, so I'll spell that out. A city name, organization name, and I'll just fill in some values here. And the common name is important because if people will be entering, let's say, www.site.com in a web browser to access my secured web server, then I'm going to have to make sure that that is in the certificate. So I'm going to put that in as the Common Name, for the Email Address, maybe admin@fakeco.com.

He types CA for the Country Name (2 letter code) [AU] and presses the Enter key. The output prompts: State or Province Name (full name) [Some-State] and he types Nova Scotia. He presses the Enter key, the output prompts: Locality Name (eg, city) [], and he types Halifax. He presses the Enter key, the output prompts: Organization Name (eg, company) [Internet Widgits Pty Ltd], and he types FakeCo. He presses the Enter key, the output prompts: Organizational Unit Name (eg, section) [], and he types Hq. He presses the Enter key, the output prompts: Common Name (e.g. server FQDN or YOUR name) [], and he types www.site.com. 

Now, when you do this with public certificate authorities out on the internet, you don't just make this stuff up. It has to be filled in correctly. So do I want a challenge password to be used with this signing request? No, so I'll just press Enter, optional company name, Enter.

And if I clear the screen and type ls, we now have a certificate signing request file, csr.

So as you might have guessed, the next order of business is we need to ask the certificate authority, which is on the same host, it doesn't have to be. But we've got the private key for the certificate authority and the certificate file for a certificate authority I've previously created already available here.

He highlights the private key: ca_priv.key and certificate file: ca.pem in the output. 

 

So that in conjunction with the certificate signing request will allow us to actually generate a usable PKI certificate for the web server.

In the real world, when you request a certificate from a public authority, they'll do some due diligence to make sure that the information you provided is accurate and correct. So trust is very important with this. Now notice here, I've got a file I've already prepared called otherinfo.ext. It's extension, or additional information, you can think of it as a template that will be used to issue certificates. And if I used the cat command to display the contents of that file, you're going to see what I've typed in here.

So I've typed in a bunch of stuff, notably, keyUsage, how can this be used, for digitalSignature, for keyEncipherment. In other words, for encryption of keys and also for the encryption of data, dataEncipherment. But what's really important here is the subjectAltName, san, S-A-N which is referring to a section further in the file called @alt_names. And here it is down here. So the first DNS name, DNS.1 is going to be the URL people will enter to connect to my site, www.site.com. So I actually want that injected into the certificate that we're about to issue. Okay, so let's just clear the screen, let's just do an ls so we can see our file names again here.

So I've created the command here. It's openssl x509, that's the type of certificate we're creating, a PKI certificate. We are looking at a request for an actual certificate, and we're getting that from our certificate signing request input file, so -in. And I'm just referring to the name of the CSR that we generated. Then I'm going to specify the CA certificate file which happens beyond this machine that's called ca.pem, then the CA private key file, same thing. It's on this machine.

I'll specify that and then the output file I want to create here which is my actual web server certificate file, the number of days that the web server certificate will be valid. So here it's only one year, 365 days. The signature algorithm that will be used by the CA to digitally sign the certificate, in this case, sha256 is the algorithm that will be used, telling it to create a serial number. Because PKI certificates are uniquely tracked by a serial number and then I'm specifying to use that otherinfo.ext file with the extensions file parameter -ext file. And remember, that maps our subject name to our alternative name, so the URL of the host. And I'm going to go ahead and press Enter.

He executes the following command: openssl x509 -req -in www.site.com.csr -CA ca.pem -CAkey ca_priv.key -out www.site.com.crt -days 365 -sha256 -CAcreateserial -extfile otherinfo.ext. The output prompts: Enter pass phrase for ca_priv.key. 

So of course, it wants the pass phrase for the CA private key, I'm going to go ahead and specify that value, and then I'll press Enter. And that's it, it's done. If I clear the screen with the clear command and type ls, we've now got an actual PKI certificate file that can be used, in this case, for a web server, such as with the Apache web server.

So then when we configure our web server engine whether it's nginx or Apache or whatever it is to use that as the PKI file to secure the connection normally over port 443.

 


Python Tech Courses

Pluralsight OS module

 

os.chdir('/' + environment + '/sasdata/EWST/automation/rfw_batch_scheduler/code')

os.getcwd()

 

Pluralsight CSV:

for file_to_process in files_to_process:

 with open(file_to_process) as csv_file:

  csv_reader = csv.DictReader(csv_file, delimiter=',', quotechar='"')

  for row in csv_reader:

   print(row['startdate'] + ' ' + row['schedulehr'] + ' ' + row['schedulemin'])

 


import datetime 
today = datetime.date.today() 
yesterday = today - datetime.timedelta(days=l)
tomorrow = today + datetime.timedelta(days=l) 
print yesterday, today, tomorrow 
#emits: 2004-11-17 2004-11-18 2004-11-19 


#Note output will be object
>>> anniversary =  today + datetime.timedelta(days=365)  #add 1 year 
>>> print anniversary 
2005-11-18 

>>> t = datetime.datetime.today()  # get right now
>>> t
datetime.datetime(2004, 11, 19, 10, 12, 43, 8010000)

>>>  t2 = t + datetime.timedelta(seconds=1) # add 1 second 
>>>  t2
datetime.datetime(2004, 11, 19, 10, 12, 44, 8010000)


>>>  t3 = t + datetime.timedelta(seconds=3600) # add 1 hour
>>>  t3
datetime.datetime(2004, 11, 19, 11, 12, 44, 8010000)


Python Code to schedule a process:
from __future__ import print_function
from dateutil import rrule
import datetime
import csv
import os, sys, stat, glob
import shutil
import subprocess
environment="prd"

#To calculate the minutes to sleep
def mins_between(start_date, end_date):
 mins=rrule.rrule(rrule.MINUTELY, dtstart=start_date, until=end_date)
 return mins.count()

while True:
  os.chdir('/' + environment + '/sasdata/testautomation/project/input')
  files_to_process=glob.glob("*csv")
  for file_to_process in files_to_process:
   with open(file_to_process) as csv_file:
    csv_reader = csv.DictReader(csv_file, delimiter=',', quotechar='"')
    for row in csv_reader:
     session_id=row['session_id']
     userid=session_id.split("_")[2]
     startdate=row['startdate']
     dd=startdate[:2]
     mm=startdate[2:4]
     yyyy=str(20)+startdate[4:6]
     hr=row['schedulehr']
     mn=row['schedulemin']
     time_to_sleep=mins_between(datetime.datetime.today(), datetime.datetime(int(yyyy),int(mm),int(dd),int(hr),int(mn)))
     #print(str(time_to_sleep) + " " + file_to_process)
     if time_to_sleep > 0:
      #print(str(time_to_sleep) + " " + file_to_process + " " + session_id)
      f1=open('/' + environment + '/sasdata/testautomation/project/jobs/' + file_to_process, 'w+')
      print("sleep " + str(time_to_sleep) + "m", file=f1)
      print('sudo -u ' + userid + " " +  "/prd/sasconfig/Lev1/SASApp/BatchServer/sasbatch.sh /prd/sasdata/testsasmacros/RFW_TASK_Execution.sas -sysparm " + session_id + " -log /prd/sasdata/testautomation/project/log/" + session_id + ".log", file=f1)
      f1.close()
      os.chmod('/' + environment + '/sasdata/testautomation/project/jobs/' + file_to_process, stat.S_IRWXU | stat.S_IRWXG)
      script_to_execute='/' + environment + '/sasdata/testautomation/project/jobs/' + file_to_process
      os.system(script_to_execute + ' &')
      #subprocess.call([script_to_execute])
      shutil.move('/' + environment + '/sasdata/testautomation/project/input/' + file_to_process, '/' + environment + '/sasdata/testautomation/project/processed/' + file_to_process)
     else:
      shutil.move('/' + environment + '/sasdata/testautomation/project/input/' + file_to_process, '/' + environment + '/sasdata/testautomation/project/failed/' + file_to_process)

Saturday, 13 June 2020

Docker - Notes

Debugging:

If docker is not starting then run the command "dockerd" in terminal, it will show the logs in the terminal.

Settings are present in /etc/docker. There will be two json key.json and daemon.json.

If docker container failed with exit (1) then check "docker logs <container_id>"

 

Docker Postgres

infra1@master-node[~] $ sudo docker run -d -p8001:5432 -e POSTGRES_PASSWORD=postgres -v /home/infra1/postgres/data:/var/lib/postgresql/data artifactory.global.com/postgres:alpine

 sudo docker run -d --name mysql_for_pydio -e MYSQL_ROOT_PASSWORD=password  artifactory.global.com/mysql

To get inside the container:

infra1@master-node[~] $ docker exec -it 942102da94c0 /bin/bash

 

 

 

 

 

Pluralsight getting started:

 

To get version. Docker installation consists of server, API and client. Client is the CLI which we use and server is core engine. API calls were made to server whenever we run a command from Docker CLI.

root@academy-rhel7[axessuser] # docker version

Client:

 Version:         1.12.6

 API version:     1.24

 Package version: docker-common-1.12.6-11.el7.x86_64

 Go version:      go1.7.4

 Git commit:      96d83a5/1.12.6

 Built:           Thu Feb 23 11:52:33 2017

 OS/Arch:         linux/amd64

 

Server:

 Version:         1.12.6

 API version:     1.24

 Package version: docker-common-1.12.6-11.el7.x86_64

 Go version:      go1.7.4

 Git commit:      96d83a5/1.12.6

 Built:           Thu Feb 23 11:52:33 2017

 OS/Arch:         linux/amd64

 

Info commands show the containers running.

root@academy-rhel7[axessuser] # docker info

Containers: 0

 Running: 0

 Paused: 0

 Stopped: 0

Images: 0

Server Version: 1.12.6

Storage Driver: devicemapper

 Pool Name: rootvg-docker--pool

 Pool Blocksize: 524.3 kB

 Base Device Size: 10.74 GB

 Backing Filesystem: xfs

 Data file:

 Metadata file:

 Data Space Used: 20.45 MB

 Data Space Total: 7.68 GB

 Data Space Available: 7.659 GB

 Metadata Space Used: 40.96 kB

 Metadata Space Total: 54.53 MB

 Metadata Space Available: 54.48 MB

 Thin Pool Minimum Free Space: 767.6 MB

 Udev Sync Supported: true

 Deferred Removal Enabled: true

 Deferred Deletion Enabled: false

 Deferred Deleted Device Count: 0

 Library Version: 1.02.135-RHEL7 (2016-11-16)

Logging Driver: journald

Cgroup Driver: systemd

Plugins:

 Volume: local

 Network: bridge host overlay null

 Authorization: rhel-push-plugin

Swarm: inactive

Runtimes: docker-runc runc

Default Runtime: docker-runc

Security Options: seccomp selinux

Kernel Version: 3.10.0-514.26.1.el7.x86_64

Operating System: Red Hat Enterprise Linux Server 7.2 (Maipo)

OSType: linux

Architecture: x86_64

Number of Docker Hooks: 2

CPUs: 2

Total Memory: 1.796 GiB

Name: academy-rhel7.localdomain

ID: J6CQ:RQHG:2JKT:IKNU:L5BQ:7DQD:OT3T:ZALJ:KA55:PKEN:2T3S:GGSS

Docker Root Dir: /var/lib/docker

Debug Mode (client): false

Debug Mode (server): false

Registry: https://artifactory.global.com/v1/

WARNING: bridge-nf-call-iptables is disabled

WARNING: bridge-nf-call-ip6tables is disabled

Insecure Registries:

 docker-registry-default.apps.dtp-np.ocp.com

 172.30.0.0/16

 127.0.0.0/8

Registries: artifactory.global.com (secure), docker.io (secure)

 

Run command is used to start docker process. Without any option to run command it will login to the container and execute the script. In below case the image is not present locally so the Docker server automatically pulled from Artifactory

root@academy-rhel7[axessuser] # docker run hello-world

Unable to find image 'hello-world:latest' locally

Trying to pull repository artifactory.global.com/hello-world ..

latest: Pulling from artifactory.global.com/hello-world

1b930d010525: Pull complete

Digest: sha256:4fe721ccc2e8dc7362278a29dc660d833570ec2682f4e4194f4ee23e415e1064

 

Hello from Docker!

This message shows that your installation appears to be working correctly.

 

To generate this message, Docker took the following steps:

 1. The Docker client contacted the Docker daemon.

 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.

    (amd64)

 3. The Docker daemon created a new container from that image which runs the

    executable that produces the output you are currently reading.

 4. The Docker daemon streamed that output to the Docker client, which sent it

    to your terminal.

 

To try something more ambitious, you can run an Ubuntu container with:

 $ docker run -it ubuntu bash

 

Share images, automate workflows, and more with a free Docker ID:

 https://hub.docker.com/

 

For more examples and ideas, visit:

 https://docs.docker.com/get-started/

 

 

PS command with "a" option will show historical process that were stopped

root@academy-rhel7[axessuser] # docker ps -a

CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES

0c8a7e686341        hello-world         "/hello"            2 minutes ago       Exited (0) 2 minutes ago                       amazing_knuth

 

PS  command will show only the running process

root@academy-rhel7[axessuser] # docker ps

CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

 

Image command shows the list of image we have in local

root@academy-rhel7[axessuser] # docker images

REPOSITORY                                             TAG                 IMAGE ID            CREATED             SIZE

artifactory.global.com/hello-world   latest              fce289e99eb9        11 months ago       1.84 kB

 

Pull command will pull the image from Artifactory (link registered in registry)

root@academy-rhel7[axessuser] # docker pull alpine

Using default tag: latest

Trying to pull repository artifactory.global.com/alpine ..

latest: Pulling from artifactory.global.com/alpine

89d9c30c1d48: Pull complete

Digest: sha256:c19173c5ada610a5989151111163d28a67368362762534d8a8121ce95cf2bd5a

root@academy-rhel7[axessuser] # docker pull ubuntu

Using default tag: latest

Trying to pull repository artifactory.global.com/ubuntu ..

latest: Pulling from artifactory.global.com/ubuntu

7ddbc47eeb70: Pull complete

c1bbdc448b72: Pull complete

8c3b70e39044: Pull complete

45d437916d57: Pull complete

Digest: sha256:7c2e0528340e055e5b6d959d70a71a0a6932bd4de56385718d8e15c2295cd5a2

 

We can pull specific version using :<version number>

root@academy-rhel7[axessuser] # docker pull ubuntu:14.04

Trying to pull repository artifactory.global.com/ubuntu ..

14.04: Pulling from artifactory.global.com/ubuntu

a7344f52cb74: Pull complete

515c9bb51536: Pull complete

e1eabe0537eb: Pull complete

4701f1215c13: Pull complete

Digest: sha256:3590458403b522985068fa21888da3e351e5c72833936757c33baf9555b09e1e

 

Image command shows the list of image we have in local

root@academy-rhel7[axessuser] # docker images

REPOSITORY                                             TAG                 IMAGE ID            CREATED             SIZE

artifactory.global.com/ubuntu        latest              775349758637        6 weeks ago         64.19 MB

artifactory.global.com/alpine        latest              965ea09ff2eb        8 weeks ago         5.548 MB

artifactory.global.com/ubuntu        14.04               2c5e00d77a67        7 months ago        188.2 MB

artifactory.global.com/hello-world   latest              fce289e99eb9        11 months ago       1.84 kB

root@academy-rhel7[axessuser] # docker rmi ubuntu:14.04

Untagged: ubuntu:14.04

Untagged: artifactory.global.com/ubuntu@sha256:3590458403b522985068fa21888da3e351e5c72833936757c33baf9555b09e1e

Deleted: sha256:2c5e00d77a67934d5e39493477f262b878f127b9c01b491f06d8f06f78819578

Deleted: sha256:664a2bb343be8b1a691e0ce9c563ee654c30a1c694dc1b25ebb5467fa3d074fd

Deleted: sha256:4ac0c5a114c714b429ff471f5415974618e0b6fc743070d445cd0e2ac586cc7b

Deleted: sha256:5f200444c0009c41eb39bc1cf37a5b3c2e953478f2f373c5a1873dee820e0ced

Deleted: sha256:b057ab380990c219581e3b074919413ebe31079cbd0d615f63872c471b4dc633

 

root@academy-rhel7[axessuser] # docker images

REPOSITORY                                             TAG                 IMAGE ID            CREATED             SIZE

artifactory.global.com/ubuntu        latest              775349758637        6 weeks ago         64.19 MB

artifactory.global.com/alpine        latest              965ea09ff2eb        8 weeks ago         5.548 MB

artifactory.global.com/hello-world   latest              fce289e99eb9        11 months ago       1.84 kB

 

Below command is used to run a Docker image as daemon (-d)

-d - stands for detached. Alternatively you can attach using -it (interactive)

-name - Unique name can be anything

-p - port mapping. 80 is server and 8080 is docker

Nigelpulton - It is the docker image name. Any image with / is not official

root@academy-rhel7[axessuser] # #docker run -d --name web -p 80:8080 nigelpulton/pluralsight-docker-ci

 

 

Below command  is used to get inside the docker container. Whereas when using it will run the container and control will come back to us

root@academy-rhel7[axessuser] # docker run -it --name temp ubuntu:latest /bin/bash

root@09bd84032112:/# root@academy-rhel7[axessuser] #

 

To exit from the container use control + p + q or else the container will get closed.

root@academy-rhel7[axessuser] # #CONTROL + p + q to exit without closing the process

 

root@academy-rhel7[axessuser] # docker ps

CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

09bd84032112        ubuntu:latest       "/bin/bash"         4 minutes ago       Up 4 minutes                            temp

 

To  stop all container

root@academy-rhel7[axessuser] # docker stop $(docker ps -aq)

09bd84032112

0c8a7e686341

 

To remove all container

root@academy-rhel7[axessuser] # docker rm $(docker ps -aq)

09bd84032112

0c8a7e686341

 

root@academy-rhel7[axessuser] # docker ps -a

CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

 

root@academy-rhel7[axessuser] # docker images

REPOSITORY                                             TAG                 IMAGE ID            CREATED             SIZE

artifactory.global.com/ubuntu        latest              775349758637        6 weeks ago         64.19 MB

artifactory.global.com/alpine        latest              965ea09ff2eb        8 weeks ago         5.548 MB

artifactory.global.com/hello-world   latest              fce289e99eb9        11 months ago       1.84 kB

 

To remove all images

root@academy-rhel7[axessuser] # docker rmi $(docker images -q)

Untagged: artifactory.global.com/ubuntu:latest

Untagged: artifactory.global.com/ubuntu@sha256:7c2e0528340e055e5b6d959d70a71a0a6932bd4de56385718d8e15c2295cd5a2

Deleted: sha256:775349758637aff77bf85e2ff0597e86e3e859183ef0baba8b3e8fc8d3cba51c

Deleted: sha256:4fc26b0b0c6903db3b4fe96856034a1bd9411ed963a96c1bc8f03f18ee92ac2a

Deleted: sha256:b53837dafdd21f67e607ae642ce49d326b0c30b39734b6710c682a50a9f932bf

Deleted: sha256:565879c6effe6a013e0b2e492f182b40049f1c083fc582ef61e49a98dca23f7e

Deleted: sha256:cc967c529ced563b7746b663d98248bc571afdb3c012019d7f54d6c092793b8b

Untagged: artifactory.global.com/alpine:latest

Untagged: artifactory.global.com/alpine@sha256:c19173c5ada610a5989151111163d28a67368362762534d8a8121ce95cf2bd5a

Deleted: sha256:965ea09ff2ebd2b9eeec88cd822ce156f6674c7e99be082c7efac3c62f3ff652

Deleted: sha256:77cae8ab23bf486355d1b3191259705374f4a11d483b24964d2f729dd8c076a0

Untagged: artifactory.global.com/hello-world:latest

Untagged: artifactory.global.com/hello-world@sha256:4fe721ccc2e8dc7362278a29dc660d833570ec2682f4e4194f4ee23e415e1064

Deleted: sha256:fce289e99eb9bca977dae136fbe2a82b6b7d4c372474c9235adc1741675f587e

Deleted: sha256:af0b15c8625bb1938f1d7b17081031f649fd14e6b233688eea3c5483994a66a3

root@academy-rhel7[axessuser] # docker images

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE

 

rm vs rmi:

rm is to remove the container

rmi is to remove images

 

Container Lifecycle: Container is running images. We can start, stop until we delete using "docker rm". To delete image we need first delete container.

 


Golang - Email - Secure code warrior

 package mail import ( "net/smtp" "gobin/config" ) var ( emailConfig config.Email ) type Mail struct { Destinati...