DevOps Lesson
import socket
# Change the following host and see what IP it prints!
host = "youtube.com"
ip = socket.gethostbyname(host)
print(ip)
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((ip, 80))
print("Successfully connected!")
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((ip, 80))
# Send a GET request to "/"
s.sendall(b"GET / HTTP/1.1\r\n\r\n")
# Recieve & print 2048 bytes of data
data = s.recv(2048)
print(data.decode())
import requests
# Change the URL to whatever you'd like
response = requests.get("https://i.imgur.com/ow7kQl1.png")
print("Status code:", response.status_code)
print("Headers:", response.headers)
print("Response text:", response.text[:100])
print("Content-Type:", response.headers.get("Content-Type"))
# Add a line to print the "Content-Type" header of the response
# Try an image URL!
aws = "3.130.255.192"
response = requests.get("http://" + aws)
print(response.text)
Configuration
server {
// Listen on virtual "port 80"
listen 80;
listen [::]:80;
server_name 3.130.255.192;
location / {
// Inform server about original client
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
// Forward all requests transparently to the server running on our computer
proxy_pass http://localhost:9099;
}
}
Load Balancing
upstream example.com {
server server1.example.com;
server server1.example.com;
}
HTTP Headers
server {
add_header X-Cool-Header "I love APCSP!";
location /pages {
add_header X-Cooler-Header "This is my secret header!";
}
}
Check In
- Research 1 HTTP header and describe, in detail, its purpose.
- One HTTP header is the Content-Type header. Its purpose is to indicate the type of data that is being returned in the response. This ensures the client is able to properly interpret the data. For example, a response with a Content-Type header of "text/html; charset=UTF-8" says that the response body is in HTML format and is encoded using UTF-8.
-
Write a line in a sample NGINX configuration that will add that specific header to the
/information
locationserver { add_header Name-Header "Annika Liao"; location /information { add_header Info-Header "Header with information"; } }
- Explain the purpose of the load balancing performed by NGINX
- The load balancing in NGINX helps distribute incoming network traffic to several servers or instances. This ensures that one server is not overwhelmed with too many requests. This improves the efficiency and reliability of all the servers
- Modify the following code block to obtain the value of the secret header on
/products
of the AWS site
aws = "3.130.255.192"
response = requests.get("http://" + aws+ "/products")
secret_header = response.headers.get('X-Cooler-Header')
print("The secret header is:", secret_header)
Hacks
DNS Hacks ✔
- What does DNS stand for?
- DNS means for domain name service
- What is the purpose of DNS?
- DNS is used to translate domain names into IP addresses so that users can use the internet.
- How does DNS work?
- When a user types a domain name into the web browser, the browser sends a query to a resolver which search for the IP addresses that is associated with that domain name. The IP address is returned to the browser, connecting it to the correct server.
- What is a DNS resolver?
- Software that processes DNS queries and returns the correct address.
- Process of DNS:
HTTP hacks ✔
- Complete the above check-in questions and change the hosts (0.1)
- Complete the above code-segment to retrieve the secret header (0.1)
Bonus (0.05) ✔
Create a diagram showing the layers of abstraction that allow us to use HTTP (IP, TCP, etc.)
CORS Hacks ✔
- Explain what CORS is and what it stands for
- Cross-origin resource sharing is a set of techniques used to prevent web pages from accessing resources on other domains without permission. It adds HTTP headers to a web bpage in order to inform the browser whether access is allowed.
- Describe how you would be able to implement CORS into your own websites
- You would need to add specific HTTP headers to your server that indicate which domains/origins are allowed to access the resources. I would need to determine which origins I want or need to restrict, then use Access-Control-Allow-Origin, which specifies the domains allowed.
- Describe why you would want to implement CORS into your own websites
- By implementing CORS, I can control which domains are allowed to access my resources, and prevent unauthorized access. This may include attackers trying to steal data, or put malicious code on your site. CORS is a security measure that ensures the safety of you and all your website's users.
- How could use CORS to benefit yourself in the future?
- It is important when developing websites that your site is safe to use for everyone. CORS is a security measure towards this. Developers can make sure their sites can access resources from other domains without security issues.
Total: 0.2 points
KASM Hacks ✔
- What is the purpose of "sudo" when running commands in terminal?
- "sudo" stands for "superuser do" and is used in the terminal to run commands with elevated privileges. It allows a user to execute commands as the system's root user or another user with administrative right.
- What are some commands which allow us to look at how the storage of a machine is set up as?
- cd stands for change directory and it allows you to change your current working directory. ls is list and it lists contents of your current directory. rm stands for remove and it can delete a file or directory. mkdir is make directory and creates a new directory.
- What do you think are some alternatives to running "curl -O" to get the zip file for KASM?
- You can download a file manually from the site and save it locally.
- What kind of commands do you think the "install.sh" command has and why is it necessary to call it?
- install.sh likely contains commands necessary to install KASM software on local machine. It is necessary because it contains commands necessary to install software or packages.
- Explain in at least 3-4 sentences how deploying KASM is related to/requires other topics talked about in the lesson and/or potential ways to add things mentioned in the lesson to this guide.
- Deploying KASM, a container-based streaming service, requires several components such as headers, NGINX, load balancing, configuration, DNS, and CORS. These components are important for setting up the KASM service, configuring security settings, managing incoming web traffic, distributing traffic across multiple server instances, mapping the KASM domain name to its IP address, and enabling cross-origin resource sharing. Understanding these components is necessary to deploy KASM successfully.
Total: 0.2 points