@@ -8,52 +8,112 @@ if [ -z "${JQ}" ]; then
88fi
99
1010OUTPUT=internal/shared/util/tlsprofiles/mozilla_data.go
11- INPUT=https://ssl-config.mozilla.org/guidelines/5.8 .json
11+ INPUT=https://ssl-config.mozilla.org/guidelines/latest .json
1212
1313TMPFILE=" $( mktemp) "
1414trap ' rm -rf "$TMPFILE"' EXIT
1515
16- curl -L -s ${INPUT} > ${TMPFILE}
16+ if ! curl -L -s -f " ${INPUT} " > " ${TMPFILE} " ; then
17+ echo " ERROR: Failed to download ${INPUT} (HTTP error or connection failure)" >&2
18+ exit 1
19+ fi
20+
21+ if ! ${JQ} empty " ${TMPFILE} " 2> /dev/null; then
22+ echo " ERROR: Downloaded data from ${INPUT} is not valid JSON" >&2
23+ exit 1
24+ fi
1725
18- version=$( ${JQ} -r ' .version' ${TMPFILE} )
26+ # Extract stored version from current output file (may be empty on first run)
27+ STORED_VERSION=$( grep ' ^// DATA VERSION:' " ${OUTPUT} " 2> /dev/null | awk ' {print $4}' || true)
1928
20- cat > ${OUTPUT} << EOF
29+ # Extract version from downloaded JSON and fail early if missing
30+ NEW_VERSION=$( ${JQ} -r ' .version' " ${TMPFILE} " )
31+ if [ -z " ${NEW_VERSION} " ] || [ " ${NEW_VERSION} " = " null" ]; then
32+ echo " ERROR: Could not read .version from ${INPUT} " >&2
33+ exit 1
34+ fi
35+
36+ if [ " ${NEW_VERSION} " = " ${STORED_VERSION} " ]; then
37+ echo " Mozilla TLS data is already at version ${NEW_VERSION} , skipping regeneration."
38+ exit 0
39+ fi
40+ echo " Updating Mozilla TLS data from version ${STORED_VERSION:- unknown} to ${NEW_VERSION} "
41+
42+ cat > " ${OUTPUT} " << EOF
2143package tlsprofiles
2244
2345// DO NOT EDIT, GENERATED BY ${0}
2446// DATA SOURCE: ${INPUT}
25- // DATA VERSION: ${version }
47+ // DATA VERSION: ${NEW_VERSION }
2648
2749import (
2850"crypto/tls"
2951)
3052EOF
3153
3254function generate_profile {
33- cat >> ${OUTPUT} << EOF
34-
35- var ${1} TLSProfile = tlsProfile{
55+ local profile=" ${1} "
56+
57+ # Validate the profile key exists before writing any output
58+ local exists
59+ exists=$( ${JQ} -r " .configurations | has(\" ${profile} \" )" " ${TMPFILE} " )
60+ if [ " ${exists} " != " true" ]; then
61+ echo " ERROR: Profile '${profile} ' not found in ${INPUT} (version ${NEW_VERSION} )" >&2
62+ echo " Available profiles: $( ${JQ} -r ' .configurations | keys | join(", ")' " ${TMPFILE} " ) " >&2
63+ exit 1
64+ fi
65+
66+ # Validate tls_versions is a non-empty array with a non-null first entry
67+ if ! ${JQ} -e " .configurations.${profile} .tls_versions | type == \" array\" and length > 0 and .[0] != null" " ${TMPFILE} " > /dev/null; then
68+ echo " ERROR: Missing or empty .configurations.${profile} .tls_versions[0] in ${INPUT} " >&2
69+ exit 1
70+ fi
71+
72+ # Validate that at least one cipher is present across ciphersuites and ciphers.iana
73+ # (modern has only ciphersuites; intermediate has both; either alone is valid)
74+ local cipher_count
75+ cipher_count=$( ${JQ} -r "
76+ [
77+ (.configurations.${profile} .ciphersuites // []),
78+ (.configurations.${profile} .ciphers.iana // [])
79+ ] | add | length" " ${TMPFILE} " )
80+ if [ " ${cipher_count} " -eq 0 ] 2> /dev/null; then
81+ echo " ERROR: Profile '${profile} ' has no ciphers in ciphersuites or ciphers.iana" >&2
82+ exit 1
83+ fi
84+
85+ # Validate tls_curves is non-empty
86+ local curve_count
87+ curve_count=$( ${JQ} -r " .configurations.${profile} .tls_curves | length" " ${TMPFILE} " )
88+ if [ " ${curve_count} " -eq 0 ] 2> /dev/null; then
89+ echo " ERROR: Profile '${profile} ' has no entries in tls_curves" >&2
90+ exit 1
91+ fi
92+
93+ cat >> " ${OUTPUT} " << EOF
94+
95+ var ${profile} TLSProfile = tlsProfile{
3696ciphers: cipherSlice{
3797cipherNums: []uint16{
3898EOF
3999
40- ${JQ} -r " .configurations.$1 .ciphersuites.[] | . |= \" tls.\" + . + \" ,\" " ${TMPFILE} >> ${OUTPUT}
41- ${JQ} -r " .configurations.$1 .ciphers.iana[] | . |= \" tls.\" + . + \" ,\" " ${TMPFILE} >> ${OUTPUT}
100+ ${JQ} -r " ( .configurations.${profile} .ciphersuites // [])[] | . |= \" tls.\" + . + \" ,\" " " ${TMPFILE} " >> " ${OUTPUT} "
101+ ${JQ} -r " ( .configurations.${profile} .ciphers.iana // [])[] | . |= \" tls.\" + . + \" ,\" " " ${TMPFILE} " >> " ${OUTPUT} "
42102
43- cat >> ${OUTPUT} << EOF
103+ cat >> " ${OUTPUT} " << EOF
44104},
45105},
46106curves: curveSlice{
47107curveNums: []tls.CurveID{
48108EOF
49109
50- ${JQ} -r " .configurations.$1 .tls_curves[] | . |= . + \" ,\" " ${TMPFILE} >> ${OUTPUT}
110+ ${JQ} -r " .configurations.${profile} .tls_curves[] | . |= . + \" ,\" " " ${TMPFILE} " >> " ${OUTPUT} "
51111
52- version=$( ${JQ} -r " .configurations.$1 .tls_versions[0]" ${TMPFILE} )
112+ version=$( ${JQ} -r " .configurations.${profile} .tls_versions[0]" " ${TMPFILE} " )
53113 version=${version/ TLSv1./ tls.VersionTLS1}
54114 version=${version/ TLSv1/ tls.VersionTLS10}
55115
56- cat >> ${OUTPUT} << EOF
116+ cat >> " ${OUTPUT} " << EOF
57117},
58118},
59119minTLSVersion: ${version} ,
63123
64124generate_profile " modern"
65125generate_profile " intermediate"
66- generate_profile " old"
67126
68- # Remove unsupported ciphers from Go's crypto/tls package (Mozilla v5.8 includes these but Go doesn't support them)
69- sed -i.bak ' /TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384/d; /TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384/d; /TLS_RSA_WITH_AES_256_CBC_SHA256/d' ${OUTPUT}
70- rm -f ${OUTPUT} .bak
127+ # Remove unsupported ciphers from Go's crypto/tls package
128+ sed -i.bak ' /TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384/d; /TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384/d; /TLS_RSA_WITH_AES_256_CBC_SHA256/d' " ${OUTPUT} "
129+ rm -f " ${OUTPUT} .bak"
71130
72131# Make go happy
73- go fmt ${OUTPUT}
132+ go fmt " ${OUTPUT} "
0 commit comments