Skip to content

Instantly share code, notes, and snippets.

@Gerg
Last active December 14, 2021 21:07
Show Gist options
  • Save Gerg/1584692d94222ff2fdc8d8b5ee5eadbc to your computer and use it in GitHub Desktop.
Save Gerg/1584692d94222ff2fdc8d8b5ee5eadbc to your computer and use it in GitHub Desktop.
Python 3 script for detecting Cloud Foundry droplets using the java buildpack (for log4j reasons).
#!/usr/bin/env python3
# Usage:
# ./java_app_sniffer.py
#
# Environment Variables (optional):
# PAGE_SIZE - Specifies how many droplets to retrieve per API request (Default: 5000)
# BUILDPACK_NAME - Specifies the name of the java buildpack for targeted environment (Default: java_buildpack)
#
# Note:
# - Requires cf CLI to be installed and logged in as user with global read access
import collections
import json
import os
import shutil
import subprocess
import sys
droplet_data = collections.defaultdict(list)
def get_page(page: int, page_size: str, buildpack_name: str) -> bool:
out = subprocess.run(
["cf", "curl", f"/v3/droplets?per_page={page_size}&page={page}"],
text=True,
capture_output=True,
)
if out.returncode != 0:
sys.exit(out.stderr)
response = json.loads(out.stdout)
pagination = response.get("pagination")
resources = response.get("resources")
if pagination == None or resources == None:
sys.exit("Invalid response from API " + out.stdout)
for droplet in resources:
try:
if droplet.get("lifecycle").get("type") == "docker":
continue
buildpacks = droplet.get("buildpacks")
buildpack_names = [buildpack.get("name") for buildpack in buildpacks]
if buildpack_name in buildpack_names:
app_guid = (
droplet.get("relationships").get("app").get("data").get("guid")
)
droplet_data[app_guid].append(
{
"droplet_guid": droplet.get("guid"),
"buildpacks": buildpacks,
}
)
except BaseException as err:
print("Error parsing " + json.dumps(droplet))
raise
return pagination.get("next") != None
def display_results():
app_title = "App Guid"
droplet_title = "Droplet Guid"
buildpacks_title = "Buildpacks"
print(f"\n{app_title:40}{droplet_title:40}{buildpacks_title:100}")
for app_guid, droplets in droplet_data.items():
for droplet in droplets:
try:
droplet_guid = droplet.get("droplet_guid")
buildpacks = ",".join(
[
"=".join(
(
str(buildpack.get("name", "unknown")),
str(buildpack.get("version", "unknown")),
)
)
for buildpack in droplet.get("buildpacks")
]
)
print(f"{app_guid:40}{droplet_guid:40}{buildpacks:100}")
except BaseException as err:
print("Error displaying " + json.dumps(droplet))
raise
def main():
if not shutil.which("cf"):
print("Requires CF CLI")
print(
"Make sure you are logged in as a user with global read permissions (admin, admin_read_only, or global_auditor)."
)
page_size = os.getenv("PAGE_SIZE", "5000")
buildpack_name = os.getenv("BUILDPACK_NAME", "java_buildpack")
page = 1
get_more_pages = True
while get_more_pages:
get_more_pages = get_page(page, page_size, buildpack_name)
page = page + 1
display_results()
if __name__ == "__main__":
main()
# Copyright 2021 Greg Cobb
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment