Skip to content

Commit

Permalink
2024-09-18 17:12:23.129943 new snippets
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardocerqueira committed Sep 18, 2024
1 parent 31ce61d commit 7ce95a3
Show file tree
Hide file tree
Showing 15 changed files with 643 additions and 560 deletions.
29 changes: 29 additions & 0 deletions seeker/report.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,32 @@
--------------------------------------------------------------------------------
2024-09-18 17:12:23.129943
--------------------------------------------------------------------------------
On branch main
Your branch is up to date with 'origin/main'.

Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
deleted: snippet/base_conversions.py
deleted: snippet/generate_signed_urls.py
deleted: snippet/ipynb_importParentDir_pyCodeTemplate.py
deleted: snippet/main.java
modified: snippet/main.py
deleted: snippet/module_5_2.py
deleted: snippet/program.java

Untracked files:
(use "git add <file>..." to include in what will be committed)
snippet/Insert Shapes in Word using Java.java
snippet/angular.Dockerfile
snippet/aws_enum.sh
snippet/check-artifact-registry-batch-deletions.py
snippet/install-awscli.sh
snippet/kafka-move-leadership.sh
snippet/spring.Dockerfile

no changes added to commit (use "git add" and/or "git commit -a")

--------------------------------------------------------------------------------
2024-09-17 17:12:22.213604
--------------------------------------------------------------------------------
Expand Down
37 changes: 37 additions & 0 deletions seeker/snippet/Insert Shapes in Word using Java.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//date: 2024-09-18T16:54:15Z
//url: https://api.github.com/gists/133d9815f8bd9b36601b1925952eeb4c
//owner: https://api.github.com/users/aspose-com-kb

import com.aspose.words.*;

public class Main
{
public static void main(String[] args) throws Exception // Adding shapes in Java
{
// Set the licenses
new License().setLicense("License.lic");

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

//Inline shape
Shape shape = builder.insertShape(ShapeType.LINE, 200, 200);
shape.setRotation(35.0);

//Free-floating shape
shape = builder.insertShape
( ShapeType.ARROW,RelativeHorizontalPosition.PAGE,250,
RelativeVerticalPosition.PAGE,150,150,150,WrapType.INLINE);
shape.setRotation(40.0);
builder.writeln();
OoxmlSaveOptions saveOptions = new OoxmlSaveOptions(SaveFormat.DOCX);

// Save shapes as DML
saveOptions.setCompliance(OoxmlCompliance.ISO_29500_2008_TRANSITIONAL);

// Save the document
doc.save("output.docx", saveOptions);

System.out.println("Shapes added successfully");
}
}
29 changes: 29 additions & 0 deletions seeker/snippet/angular.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#date: 2024-09-18T17:05:04Z
#url: https://api.github.com/gists/124d04536cdeca0cc709c6b43ffd9871
#owner: https://api.github.com/users/Riko07br

# Build angular------------------
FROM node:lts-alpine3.20 as build

RUN npm install -g @angular/cli

WORKDIR /app

COPY package.json .

RUN npm install

COPY . .

RUN npm run build

# NGINX runner---------------
FROM nginx:1.21-alpine

COPY --from=build /app/dist/frontend/browser /usr/share/nginx/html

COPY ./nginx.conf /etc/nginx/conf.d/default.conf

EXPOSE 80 4200

CMD ["nginx", "-g", "daemon off;"]
61 changes: 61 additions & 0 deletions seeker/snippet/aws_enum.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#date: 2024-09-18T16:53:38Z
#url: https://api.github.com/gists/04d0e48c523b9b73b2f1d14d81e2ba3f
#owner: https://api.github.com/users/h00die

EC2_TOKEN=$(curl -X PUT "http: "**********": 21600" 2>/dev/null || wget -q -O - --method PUT "http://169.254.169.254/latest/api/token" --header "X-aws-ec2-metadata-token-ttl-seconds: 21600" 2>/dev/null)
HEADER="X-aws-ec2-metadata-token: "**********"
URL="http://169.254.169.254/latest/meta-data"
aws_req=""
if [ "$(command -v curl)" ]; then
aws_req="curl -s -f -H '$HEADER'"
elif [ "$(command -v wget)" ]; then
aws_req="wget -q -O - -H '$HEADER'"
else
echo "Neither curl nor wget were found, I can't enumerate the metadata service :("
fi
printf "ami-id: "; eval $aws_req "$URL/ami-id"; echo ""
printf "instance-action: "; eval $aws_req "$URL/instance-action"; echo ""
printf "instance-id: "; eval $aws_req "$URL/instance-id"; echo ""
printf "instance-life-cycle: "; eval $aws_req "$URL/instance-life-cycle"; echo ""
printf "instance-type: "; eval $aws_req "$URL/instance-type"; echo ""
printf "region: "; eval $aws_req "$URL/placement/region"; echo ""
echo ""
echo "Account Info"
eval $aws_req "$URL/identity-credentials/ec2/info"; echo ""
eval $aws_req "http://169.254.169.254/latest/dynamic/instance-identity/document"; echo ""
echo ""
echo "Network Info"
for mac in $(eval $aws_req "$URL/network/interfaces/macs/" 2>/dev/null); do
echo "Mac: $mac"
printf "Owner ID: "; eval $aws_req "$URL/network/interfaces/macs/$mac/owner-id"; echo ""
printf "Public Hostname: "; eval $aws_req "$URL/network/interfaces/macs/$mac/public-hostname"; echo ""
printf "Security Groups: "; eval $aws_req "$URL/network/interfaces/macs/$mac/security-groups"; echo ""
echo "Private IPv4s:"; eval $aws_req "$URL/network/interfaces/macs/$mac/ipv4-associations/"; echo ""
printf "Subnet IPv4: "; eval $aws_req "$URL/network/interfaces/macs/$mac/subnet-ipv4-cidr-block"; echo ""
echo "PrivateIPv6s:"; eval $aws_req "$URL/network/interfaces/macs/$mac/ipv6s"; echo ""
printf "Subnet IPv6: "; eval $aws_req "$URL/network/interfaces/macs/$mac/subnet-ipv6-cidr-blocks"; echo ""
echo "Public IPv4s:"; eval $aws_req "$URL/network/interfaces/macs/$mac/public-ipv4s"; echo ""
echo ""
done
echo ""
echo "IAM Role"
eval $aws_req "$URL/iam/info"
for role in $(eval $aws_req "$URL/iam/security-credentials/" 2>/dev/null); do
echo "Role: $role"
eval $aws_req "$URL/iam/security-credentials/$role"; echo ""
echo ""
done
echo ""
echo "User Data"
# Search hardcoded credentials
eval $aws_req "http://169.254.169.254/latest/user-data"
echo ""
echo "EC2 Security Credentials"
eval $aws_req "$URL/identity-credentials/ec2/security-credentials/ec2-instance"; echo ""ty-credentials/ec2/security-credentials/ec2-instance"; echo ""
110 changes: 0 additions & 110 deletions seeker/snippet/base_conversions.py

This file was deleted.

101 changes: 101 additions & 0 deletions seeker/snippet/check-artifact-registry-batch-deletions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
#date: 2024-09-18T16:58:56Z
#url: https://api.github.com/gists/cc77d7c98cd4c0c02f40f0e777ec3c0c
#owner: https://api.github.com/users/philwhiteuk

from collections import defaultdict
from datetime import date, datetime, timedelta, UTC
import os
import textwrap

from dotenv import load_dotenv
from google.cloud import artifactregistry_v1 as artifactregistry, logging
import yaml

load_dotenv() # take environment variables from .env.

project_id = os.getenv('PROJECT_ID')
location = os.getenv('LOCATION')

def get_log(log_date: date = date.today()):
log_file_path = os.path.join(os.path.dirname(__file__), '../var/log', f'{log_date.isoformat()}.yaml')

if not os.path.exists(log_file_path):
print(f'Fetching log for {log_date.isoformat()}...')
logging_client = logging.Client(project=project_id)
log_query = f'''
protoPayload.methodName="google.devtools.artifactregistry.v1.ArtifactRegistry.BatchDeleteVersions"
AND protoPayload.authenticationInfo.principalEmail:"@gcp-sa-artifactregistry.iam.gserviceaccount.com"
AND protoPayload.request.parent:"projects/{project_id}/locations/{location}"
AND protoPayload.request.validateOnly=true
AND protoPayload.serviceName="artifactregistry.googleapis.com"
AND
(timestamp > {log_date.isoformat()} AND timestamp <= {(log_date + timedelta(days=1)).isoformat()})
'''
with open(log_file_path, 'a') as f:
for entry in logging_client.list_entries(filter_=log_query):
f.write('---\n') # Separate documents with '---'
yaml.dump(entry.to_api_repr(), f) # Write entry to file

with open(log_file_path, 'r') as f:
yaml_docs = yaml.safe_load_all(f)
return list(yaml_docs)

def get_current_inventory(repository: str, package: str):
inventory_path = os.path.join(os.path.dirname(__file__), '../var/inventory', f'{date.today().isoformat()}-{repository}-{package}.yaml')

if not os.path.exists(inventory_path):
print(f'Fetching most recent inventory for {repository}: {package}...')
artifactregistry_client = artifactregistry.ArtifactRegistryClient()
parent = f'projects/{project_id}/locations/{location}/repositories/{repository}/packages/{package}'
request = artifactregistry.ListVersionsRequest(parent=parent)
with open(inventory_path, 'a') as f:
for entry in artifactregistry_client.list_versions(request=request):
f.write('---\n') # Separate documents with '---'
yaml.dump({'name': entry.name, 'create_time': entry.create_time.isoformat(), 'update_time': entry.update_time.isoformat()}, f) # Write entry to file

with open(inventory_path, 'r') as f:
yaml_docs = yaml.safe_load_all(f)
return list(yaml_docs)

def main():
today = date.today()
print(f'Checking log for {today.isoformat()}...')
log_entries = get_log(log_date=today)

versions_marked_for_deletion = set()
for log_entry in log_entries:
if log_entry['protoPayload']['request']['names']:
versions_marked_for_deletion.update(log_entry['protoPayload']['request']['names'])

versions_by_package = defaultdict(list)
for tag in versions_marked_for_deletion:
repository = tag.split('/')[5]
package = tag.split('/')[7]
versions_by_package[f'{repository}/{package}'].append(tag)

now = datetime.now(UTC)
for package, versions_marked_for_deletion in versions_by_package.items():
repository = package.split('/')[0]
package = package.split('/')[1]

all_versions = get_current_inventory(repository=repository, package=package)
assert len(versions_marked_for_deletion) <= len(all_versions) - 100, f'{repository}/{package} is keeping fewer than the minimum 100 versions!'
summary = f'''
Resource: projects/{project_id}/locations/{location}/repositories/{repository}/package/{package}
Total files: {len(all_versions)}
Marked for deletion: {len(versions_marked_for_deletion)}
'''
print(textwrap.dedent(summary))

inventory_lookup = dict()
for item in all_versions:
inventory_lookup.update([{ item['name'], datetime.fromisoformat(item['update_time']) }])

tag_counter = 0
for tag in versions_marked_for_deletion:
if tag in inventory_lookup:
timedelta = now - inventory_lookup[tag]
assert timedelta.days >= 5, f'Version {tag} is newer than 5 days!'
print(f'- ✅ {tag.split('/')[-1]} {timedelta.days} days old')
tag_counter += 1

Loading

0 comments on commit 7ce95a3

Please sign in to comment.