<commit_before>package nginx

import (
	"bytes"
	"encoding/json"
	"fmt"
	"os"
	"os/exec"
	"path"
	"reflect"
	"text/template"

	"github.com/golang/glog"
)

const mainConfFilename = "/etc/nginx/nginx.conf"

// Configuration Type for NGINX Server
type Configuration uint8

const (
	// LocalCfg - running test on local machine (no NGINX server)
	LocalCfg = Configuration(iota)
	// StreamCfg - NGINX server configuration for TCP/UDP Streams only
	StreamCfg
	// HTTPCfg - NGINX server configuration for HTTP only
	HTTPCfg
	// StreamHTTPCfg - NGINX server configuration for TCP/UDP Streams and HTTP services
	StreamHTTPCfg
)

// NginxController Updates NGINX configuration, starts and reloads NGINX
type NginxController struct {
	nginxConfdPath string
	nginxCertsPath string
	cfgType        Configuration
	mainCfg        *NginxMainConfig
}

// NginxMainConfig describe the main NGINX configuration file
type NginxMainConfig struct {
	// Context: main directives
	Daemon         bool
	ErrorLogFile   string
	ErrorLogLevel  string
	Environment    map[string]string
	LockFile       string
	PidFile        string
	User           string
	Group          string
	WorkerPriority string
	// TODO: This needs to be a ConfigMap entry or CLI flag so that we can make
	//       it a function of the number of CPUs/vCPUs, and configure the POD
	//       resource limits propotionally for the scheduler.  For now this
	//       *should probably not* be set to 'auto'
	WorkerProcesses  string
	WorkingDirectory string

	EventContext NginxMainEventConfig

	DefaultStreamContext bool
	DefaultHTTPContext   bool
	HTTPContext          NginxMainHTTPConfig
}

// NginxMainEventConfig describe the main NGINX configuration file's 'events' context
type NginxMainEventConfig struct {
	// Context: events directives
	AcceptMutex       bool
	AcceptMutexDelay  string
	MultiAccept       bool
	WorkerConnections string
}

// NginxMainHTTPConfig describe the main NGINX configuration file's 'http' context
type NginxMainHTTPConfig struct {
	ServerNamesHashBucketSize string
	ServerNamesHashMaxSize    string
	LogFormat                 string
	HealthStatus              bool
	HealthPort                int
	HTTPSnippets              []string
	// http://nginx.org/en/docs/http/ngx_http_ssl_module.html
	SSLProtocols           string
	SSLPreferServerCiphers bool
	SSLCiphers             string
	SSLDHParam             string
}

// NewNginxController creates a NGINX controller
func NewNginxController(cfgType Configuration, nginxConfPath string, healthCheck bool, healthPort int) (*NginxController, error) {
	ngxc := NginxController{
		nginxConfdPath: path.Join(nginxConfPath, "conf.d"),
		nginxCertsPath: path.Join(nginxConfPath, "ssl"),
		cfgType:        cfgType,
		mainCfg:        nil,
	}

	if cfgType != LocalCfg {
		cfg := &NginxMainConfig{
			Daemon:          true,
			ErrorLogFile:    "/var/log/nginx/error.log",
			ErrorLogLevel:   "warn",
			PidFile:         "/var/run/nginx.pid",
			User:            "nginx",
			Group:           "nginx",
			WorkerProcesses: "2",
			/* For future use potentially, can be scrubbed if preferred.
			Environment: map[string]string{
				"OPENSSL_ALLOW_PROXY_CERTS": "1",
			}, */
		}
		switch cfgType {
		case StreamCfg:
			cfg.DefaultStreamContext = true
			cfg.DefaultHTTPContext = false
		case HTTPCfg:
			createDir(ngxc.nginxCertsPath)
			cfg.DefaultStreamContext = false
			cfg.DefaultHTTPContext = true
			cfg.HTTPContext.ServerNamesHashMaxSize = NewDefaultHTTPContext().MainServerNamesHashMaxSize
		}

		cfg.HTTPContext.HealthStatus = healthCheck
		cfg.HTTPContext.HealthPort = healthPort

		ngxc.mainCfg = cfg
		ngxc.UpdateMainConfigFile()
	}<commit_msg>use root user<commit_after>package nginx

import (
	"bytes"
	"encoding/json"
	"fmt"
	"os"
	"os/exec"
	"path"
	"reflect"
	"text/template"

	"github.com/golang/glog"
)

const mainConfFilename = "/etc/nginx/nginx.conf"

// Configuration Type for NGINX Server
type Configuration uint8

const (
	// LocalCfg - running test on local machine (no NGINX server)
	LocalCfg = Configuration(iota)
	// StreamCfg - NGINX server configuration for TCP/UDP Streams only
	StreamCfg
	// HTTPCfg - NGINX server configuration for HTTP only
	HTTPCfg
	// StreamHTTPCfg - NGINX server configuration for TCP/UDP Streams and HTTP services
	StreamHTTPCfg
)

// NginxController Updates NGINX configuration, starts and reloads NGINX
type NginxController struct {
	nginxConfdPath string
	nginxCertsPath string
	cfgType        Configuration
	mainCfg        *NginxMainConfig
}

// NginxMainConfig describe the main NGINX configuration file
type NginxMainConfig struct {
	// Context: main directives
	Daemon         bool
	ErrorLogFile   string
	ErrorLogLevel  string
	Environment    map[string]string
	LockFile       string
	PidFile        string
	User           string
	Group          string
	WorkerPriority string
	// TODO: This needs to be a ConfigMap entry or CLI flag so that we can make
	//       it a function of the number of CPUs/vCPUs, and configure the POD
	//       resource limits propotionally for the scheduler.  For now this
	//       *should probably not* be set to 'auto'
	WorkerProcesses  string
	WorkingDirectory string

	EventContext NginxMainEventConfig

	DefaultStreamContext bool
	DefaultHTTPContext   bool
	HTTPContext          NginxMainHTTPConfig
}

// NginxMainEventConfig describe the main NGINX configuration file's 'events' context
type NginxMainEventConfig struct {
	// Context: events directives
	AcceptMutex       bool
	AcceptMutexDelay  string
	MultiAccept       bool
	WorkerConnections string
}

// NginxMainHTTPConfig describe the main NGINX configuration file's 'http' context
type NginxMainHTTPConfig struct {
	ServerNamesHashBucketSize string
	ServerNamesHashMaxSize    string
	LogFormat                 string
	HealthStatus              bool
	HealthPort                int
	HTTPSnippets              []string
	// http://nginx.org/en/docs/http/ngx_http_ssl_module.html
	SSLProtocols           string
	SSLPreferServerCiphers bool
	SSLCiphers             string
	SSLDHParam             string
}

// NewNginxController creates a NGINX controller
func NewNginxController(cfgType Configuration, nginxConfPath string, healthCheck bool, healthPort int) (*NginxController, error) {
	ngxc := NginxController{
		nginxConfdPath: path.Join(nginxConfPath, "conf.d"),
		nginxCertsPath: path.Join(nginxConfPath, "ssl"),
		cfgType:        cfgType,
		mainCfg:        nil,
	}

	if cfgType != LocalCfg {
		cfg := &NginxMainConfig{
			Daemon:          true,
			ErrorLogFile:    "/var/log/nginx/error.log",
			ErrorLogLevel:   "warn",
			PidFile:         "/var/run/nginx.pid",
			User:            "root",
			Group:           "root",
			WorkerProcesses: "2",
			/* For future use potentially, can be scrubbed if preferred.
			Environment: map[string]string{
				"OPENSSL_ALLOW_PROXY_CERTS": "1",
			}, */
		}
		switch cfgType {
		case StreamCfg:
			cfg.DefaultStreamContext = true
			cfg.DefaultHTTPContext = false
		case HTTPCfg:
			createDir(ngxc.nginxCertsPath)
			cfg.DefaultStreamContext = false
			cfg.DefaultHTTPContext = true
			cfg.HTTPContext.ServerNamesHashMaxSize = NewDefaultHTTPContext().MainServerNamesHashMaxSize
		}

		cfg.HTTPContext.HealthStatus = healthCheck
		cfg.HTTPContext.HealthPort = healthPort

		ngxc.mainCfg = cfg
		ngxc.UpdateMainConfigFile()
	}
{'fraction_non_alphanumeric': 0.0685792349726776, 'fraction_numerical': 0.000819672131147541, 'mean_word_length': 3.2293471981513577, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '4842632', 'n_tokens_mistral': 2363, 'n_tokens_neox': 2203, 'n_words': 620}