github.com/breml/rootcerts@v0.2.16/generate_data.go (about)

     1  //go:build ignore
     2  // +build ignore
     3  
     4  // This program generates data.go from Mozilla Included CA Certificate List.
     5  // https://wiki.mozilla.org/CA/Included_Certificates
     6  package main
     7  
     8  import (
     9  	"bufio"
    10  	"bytes"
    11  	"fmt"
    12  	"io/ioutil"
    13  	"net/http"
    14  	"os"
    15  )
    16  
    17  // Link from: https://wiki.mozilla.org/CA/Included_Certificates
    18  // PEM of Root Certificates in Mozilla's Root Store with the Websites (TLS/SSL) Trust Bit Enabled
    19  const mozillaRootStoreWebsiteTrustBitEnabledURL = "https://ccadb-public.secure.force.com/mozilla/IncludedRootsPEMTxt?TrustBitsInclude=Websites"
    20  
    21  const header = `// Code generated by generate_data. DO NOT EDIT.
    22  
    23  // This file contains embedded root certificates from "Mozilla Included
    24  // CA Certificate List" (https://wiki.mozilla.org/CA/Included_Certificates).
    25  //
    26  // Use of these certificates is governed by Mozilla Public License 2.0
    27  // that can be found in the LICENSE.certificates file.
    28  
    29  package embedded
    30  
    31  const data = `
    32  
    33  func main() {
    34  	resp, err := http.Get(mozillaRootStoreWebsiteTrustBitEnabledURL)
    35  	if err != nil {
    36  		fail("error getting root certificates from mozilla: %v", err)
    37  	}
    38  	defer resp.Body.Close()
    39  
    40  	data, err := ioutil.ReadAll(resp.Body)
    41  	if err != nil {
    42  		fail("error reading http body: %v", err)
    43  	}
    44  
    45  	data = bytes.ReplaceAll(data, []byte("\r"), []byte{})
    46  
    47  	of, err := os.Create("embedded/data.go")
    48  	if err != nil {
    49  		fail("error creating data.go: %v", err)
    50  	}
    51  
    52  	buf := bufio.NewWriter(of)
    53  	buf.WriteString(header)
    54  
    55  	buf.WriteString("`")
    56  	buf.WriteString(string(data))
    57  	buf.WriteString("`\n")
    58  
    59  	err = buf.Flush()
    60  	if err != nil {
    61  		fail("error writing to data.go: %v", err)
    62  	}
    63  
    64  	err = of.Close()
    65  	if err != nil {
    66  		fail("error closing data.go: %v", err)
    67  	}
    68  }
    69  
    70  func fail(format string, args ...interface{}) {
    71  	fmt.Fprintf(os.Stderr, format+"\n", args...)
    72  	os.Exit(1)
    73  }