<commit_before>/*
Copyright 2018 Google LLC

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package main

import (
	"bytes"
	"encoding/base64"
	"fmt"
	"io/ioutil"
	"os"
	"os/exec"
	"strings"
	"text/template"
	"time"

	"github.com/grafeas/kritis/pkg/kritis/install"
	"github.com/sirupsen/logrus"
)

type Cert struct {
	Namespace              string
	ServiceName            string
	ServiceNameDeployments string
}

type Csr struct {
	CsrName     string
	Certificate string
}

func deleteExistingObjects() {
	csrCmd := exec.Command("kubectl", "get", "csr", csrName, "--namespace", namespace)
	csrCmd.Stderr = os.Stderr
	_, err := csrCmd.Output()
	if err == nil && createNewCSR {
		deleteCSRCmd := exec.Command("kubectl", "delete", "csr", csrName, "--namespace", namespace)
		install.RunCommand(deleteCSRCmd)
	}

	secretCmd := exec.Command("kubectl", "get", "secret", tlsSecretName, "--namespace", namespace)
	secretCmd.Stderr = os.Stderr
	_, err = secretCmd.Output()
	if err == nil {
		deleteSecretCmd := exec.Command("kubectl", "delete", "secret", tlsSecretName, "--namespace", namespace)
		install.RunCommand(deleteSecretCmd)
	}
}

func createCertificates() {
	certTmpl := Cert{namespace, serviceName, serviceNameDeployments}
	tmpl := template.New("cert")
	tmpl, err := tmpl.Parse(`{
"hosts": [
    "{{ .ServiceName }}",
    "{{ .ServiceName }}.kube-system",
    "{{ .ServiceName }}.{{ .Namespace }}",
    "{{ .ServiceName }}.{{ .Namespace }}.svc",
    "{{ .ServiceNameDeployments }}",
    "{{ .ServiceNameDeployments }}.kube-system",
    "{{ .ServiceNameDeployments }}.{{ .Namespace }}",
    "{{ .ServiceNameDeployments }}.{{ .Namespace }}.svc"

],
"key": {
	"algo": "ecdsa",
	"size": 256
}
}`)
	if err != nil {
		logrus.Fatalf("error creating template for cert: %v", err)
	}
	var tpl bytes.Buffer
	if err := tmpl.Execute(&tpl, certTmpl); err != nil {
		logrus.Fatalf("error parsing template for cert: %v", err)
	}
	cert := tpl.String()
	certCmd := exec.Command("cfssl", "genkey", "-")
	certCmd.Stdin = bytes.NewReader([]byte(cert))
	output := install.RunCommand(certCmd)

	serverCmd := exec.Command("cfssljson", "-bare", "server")
	serverCmd.Stdin = bytes.NewReader(output)
	install.RunCommand(serverCmd)
}

func createCertificateSigningRequest() {
	certificate := retrieveRequestCertificate()
	csrTmpl := Csr{csrName, certificate}
	tmpl := template.New("csr")
	tmpl, err := tmpl.Parse(`apiVersion: certificates.k8s.io/v1beta1
kind: CertificateSigningRequest
metadata:
    name: {{ .CsrName }}
spec:
    groups:
    - system:authenticated
    request: {{ .Certificate }}
    usages:<commit_msg>Fix unkeyed struct literals (govet) and initialism case.<commit_after>/*
Copyright 2018 Google LLC

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package main

import (
	"bytes"
	"encoding/base64"
	"fmt"
	"io/ioutil"
	"os"
	"os/exec"
	"strings"
	"text/template"
	"time"

	"github.com/grafeas/kritis/pkg/kritis/install"
	"github.com/sirupsen/logrus"
)

type Cert struct {
	Namespace              string
	ServiceName            string
	ServiceNameDeployments string
}

type CSR struct {
	Name        string
	Certificate string
}

func deleteExistingObjects() {
	csrCmd := exec.Command("kubectl", "get", "csr", csrName, "--namespace", namespace)
	csrCmd.Stderr = os.Stderr
	_, err := csrCmd.Output()
	if err == nil && createNewCSR {
		deleteCSRCmd := exec.Command("kubectl", "delete", "csr", csrName, "--namespace", namespace)
		install.RunCommand(deleteCSRCmd)
	}

	secretCmd := exec.Command("kubectl", "get", "secret", tlsSecretName, "--namespace", namespace)
	secretCmd.Stderr = os.Stderr
	_, err = secretCmd.Output()
	if err == nil {
		deleteSecretCmd := exec.Command("kubectl", "delete", "secret", tlsSecretName, "--namespace", namespace)
		install.RunCommand(deleteSecretCmd)
	}
}

func createCertificates() {
	certTmpl := Cert{Namespace: namespace, ServiceName: serviceName, ServiceNameDeployments: serviceNameDeployments}
	tmpl := template.New("cert")
	tmpl, err := tmpl.Parse(`{
"hosts": [
    "{{ .ServiceName }}",
    "{{ .ServiceName }}.kube-system",
    "{{ .ServiceName }}.{{ .Namespace }}",
    "{{ .ServiceName }}.{{ .Namespace }}.svc",
    "{{ .ServiceNameDeployments }}",
    "{{ .ServiceNameDeployments }}.kube-system",
    "{{ .ServiceNameDeployments }}.{{ .Namespace }}",
    "{{ .ServiceNameDeployments }}.{{ .Namespace }}.svc"

],
"key": {
	"algo": "ecdsa",
	"size": 256
}
}`)
	if err != nil {
		logrus.Fatalf("error creating template for cert: %v", err)
	}
	var tpl bytes.Buffer
	if err := tmpl.Execute(&tpl, certTmpl); err != nil {
		logrus.Fatalf("error parsing template for cert: %v", err)
	}
	cert := tpl.String()
	certCmd := exec.Command("cfssl", "genkey", "-")
	certCmd.Stdin = bytes.NewReader([]byte(cert))
	output := install.RunCommand(certCmd)

	serverCmd := exec.Command("cfssljson", "-bare", "server")
	serverCmd.Stdin = bytes.NewReader(output)
	install.RunCommand(serverCmd)
}

func createCertificateSigningRequest() {
	certificate := retrieveRequestCertificate()
	csrTmpl := CSR{Name: csrName, Certificate: certificate}
	tmpl := template.New("csr")
	tmpl, err := tmpl.Parse(`apiVersion: certificates.k8s.io/v1beta1
kind: CertificateSigningRequest
metadata:
    name: {{ .CsrName }}
spec:
    groups:
    - system:authenticated
    request: {{ .Certificate }}
    usages:
{'fraction_non_alphanumeric': 0.14979239859469817, 'fraction_numerical': 0.005110188438198659, 'mean_word_length': 4.276326874473463, 'pattern_counts': {'":': 8, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '955541', 'n_tokens_mistral': 2139, 'n_tokens_neox': 2073, 'n_words': 529}