From 437a62aaf5fbb1beb8c7d4c80b6d6ff6d5bba50d Mon Sep 17 00:00:00 2001 From: Pier-Hugues Pellerin Date: Wed, 8 Aug 2018 04:00:49 -0400 Subject: [PATCH] Assert now takes into consideration the license. (#7855) - License headers for both Elastic and ASL2 are now accessible by code using the licenses package. ``` licenses.Elastic licenses.ASL2 ``` - Added a generator to take care of syncing the files in the licenses/ directory. - Added `LICENSE` as a variable for the makefile to configure the go-licenser this is useful if a beat as a different license. --- dev-tools/cmd/asset/asset.go | 15 +++- dev-tools/cmd/license/license_generate.go | 96 +++++++++++++++++++++++ libbeat/asset/asset.go | 18 +---- libbeat/scripts/Makefile | 7 +- licenses/APACHE-LICENSE-2.0-header.txt | 16 ++++ licenses/ELASTIC-LICENSE-header.txt | 3 + licenses/license.go | 20 +++++ licenses/license_header.go | 58 ++++++++++++++ metricbeat/Makefile | 4 +- metricbeat/scripts/assets/assets.go | 2 + 10 files changed, 215 insertions(+), 24 deletions(-) create mode 100644 dev-tools/cmd/license/license_generate.go create mode 100644 licenses/APACHE-LICENSE-2.0-header.txt create mode 100644 licenses/ELASTIC-LICENSE-header.txt create mode 100644 licenses/license.go create mode 100644 licenses/license_header.go diff --git a/dev-tools/cmd/asset/asset.go b/dev-tools/cmd/asset/asset.go index 3c8d7adecbd..b412df3f91c 100644 --- a/dev-tools/cmd/asset/asset.go +++ b/dev-tools/cmd/asset/asset.go @@ -29,18 +29,21 @@ import ( "os" "github.com/elastic/beats/libbeat/asset" + "github.com/elastic/beats/licenses" ) var ( - pkg string - input string - output string + pkg string + input string + output string + license = "ASL2" ) func init() { flag.StringVar(&pkg, "pkg", "", "Package name") flag.StringVar(&input, "in", "-", "Source of input. \"-\" means reading from stdin") flag.StringVar(&output, "out", "-", "Output path. \"-\" means writing to stdout") + flag.StringVar(&license, "license", "ASL2", "License header for generated file.") } func main() { @@ -82,11 +85,17 @@ func main() { os.Exit(1) } + licenseHeader, err := licenses.Find(license) + if err != nil { + fmt.Fprintf(os.Stderr, "Invalid license: %s\n", err) + os.Exit(1) + } var buf bytes.Buffer asset.Template.Execute(&buf, asset.Data{ Beat: beatName, Name: file, Data: encData, + License: licenseHeader, Package: pkg, }) diff --git a/dev-tools/cmd/license/license_generate.go b/dev-tools/cmd/license/license_generate.go new file mode 100644 index 00000000000..11f8077dbc0 --- /dev/null +++ b/dev-tools/cmd/license/license_generate.go @@ -0,0 +1,96 @@ +// Licensed to Elasticsearch B.V. under one or more contributor +// license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright +// ownership. Elasticsearch B.V. licenses this file to you 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" + "flag" + "go/format" + "io/ioutil" + "os" + "text/template" +) + +var Template = template.Must(template.New("licenseheader").Parse(` +{{ $t := "` + "`" + `" }} +{{ .License }} + +// Code generated by beats/dev-tools/cmd/license/license_generate.go - DO NOT EDIT. + +package licenses + +import "fmt" + +{{ range $key, $value := .Licenses }} +var {{ $key }} = {{$t}} +{{ $value }}{{$t}} +{{ end -}} + +func Find(name string) (string, error) { + switch name { +{{ range $key, $value := .Licenses }} + case "{{ $key }}": + return {{ $key }}, nil +{{- end -}} + } + return "", fmt.Errorf("unknown license: %s", name) +} +`)) + +var output string + +type data struct { + License string + Licenses map[string]string +} + +func init() { + flag.StringVar(&output, "out", "license_header.go", "output file") +} + +func main() { + Headers := make(map[string]string) + content, err := ioutil.ReadFile("APACHE-LICENSE-2.0-header.txt") + if err != nil { + panic("could not read ASL2 license.") + } + Headers["ASL2"] = string(content) + + content, err = ioutil.ReadFile("ELASTIC-LICENSE-header.txt") + if err != nil { + panic("could not read Elastic license.") + } + Headers["Elastic"] = string(content) + + var buf bytes.Buffer + Template.Execute(&buf, data{ + License: Headers["ASL2"], + Licenses: Headers, + }) + + bs, err := format.Source(buf.Bytes()) + if err != nil { + panic(err) + } + + if output == "-" { + os.Stdout.Write(bs) + } else { + ioutil.WriteFile(output, bs, 0640) + } +} diff --git a/libbeat/asset/asset.go b/libbeat/asset/asset.go index afc6be73c9f..ea90a91d2d2 100644 --- a/libbeat/asset/asset.go +++ b/libbeat/asset/asset.go @@ -20,22 +20,7 @@ package asset import "text/template" var Template = template.Must(template.New("normalizations").Parse(` -// Licensed to Elasticsearch B.V. under one or more contributor -// license agreements. See the NOTICE file distributed with -// this work for additional information regarding copyright -// ownership. Elasticsearch B.V. licenses this file to you 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. +{{ .License }} // Code generated by beats/dev-tools/cmd/asset/asset.go - DO NOT EDIT. @@ -59,6 +44,7 @@ func Asset() string { `)) type Data struct { + License string Beat string Name string Data string diff --git a/libbeat/scripts/Makefile b/libbeat/scripts/Makefile index d34793664be..ceedc14ba5b 100755 --- a/libbeat/scripts/Makefile +++ b/libbeat/scripts/Makefile @@ -1,6 +1,7 @@ ### VARIABLE SETUP ### ### Application using libbeat may override the following variables in their Makefile BEAT_NAME?=libbeat## @packaging Name of the binary +LICENSE?=ASL2 BEAT_TITLE?=${BEAT_NAME}## @packaging Title of the application BEAT_PATH?=github.com/elastic/beats/${BEAT_NAME} BEAT_PACKAGE_NAME?=${BEAT_NAME} @@ -123,14 +124,14 @@ check: check-headers python-env prepare-tests ## @build Checks project and sourc check-headers: ifndef CHECK_HEADERS_DISABLED @go get github.com/elastic/go-licenser - @go-licenser -d + @go-licenser -d -license ${LICENSE} endif .PHONY: add-headers add-headers: ifndef CHECK_HEADERS_DISABLED @go get github.com/elastic/go-licenser - @go-licenser + @go-licenser -license ${LICENSE} endif .PHONY: fmt @@ -332,7 +333,7 @@ endif ifneq ($(shell [[ $(BEAT_NAME) == libbeat || $(BEAT_NAME) == metricbeat ]] && echo true ),true) mkdir -p include - go run ${ES_BEATS}/dev-tools/cmd/asset/asset.go -pkg include -in fields.yml -out include/fields.go $(BEAT_NAME) + go run ${ES_BEATS}/dev-tools/cmd/asset/asset.go -pkg include -in fields.yml -out include/fields.go $(BEAT_NAME) endif ifneq ($(shell [[ $(BEAT_NAME) == libbeat ]] && echo true ),true) diff --git a/licenses/APACHE-LICENSE-2.0-header.txt b/licenses/APACHE-LICENSE-2.0-header.txt new file mode 100644 index 00000000000..2787ffc83c5 --- /dev/null +++ b/licenses/APACHE-LICENSE-2.0-header.txt @@ -0,0 +1,16 @@ +// Licensed to Elasticsearch B.V. under one or more contributor +// license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright +// ownership. Elasticsearch B.V. licenses this file to you 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. diff --git a/licenses/ELASTIC-LICENSE-header.txt b/licenses/ELASTIC-LICENSE-header.txt new file mode 100644 index 00000000000..979c2ac8ec6 --- /dev/null +++ b/licenses/ELASTIC-LICENSE-header.txt @@ -0,0 +1,3 @@ +// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one +// or more contributor license agreements. Licensed under the Elastic License; +// you may not use this file except in compliance with the Elastic License. diff --git a/licenses/license.go b/licenses/license.go new file mode 100644 index 00000000000..43807d5248b --- /dev/null +++ b/licenses/license.go @@ -0,0 +1,20 @@ +// Licensed to Elasticsearch B.V. under one or more contributor +// license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright +// ownership. Elasticsearch B.V. licenses this file to you 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 licenses + +//go:generate go run ../dev-tools/cmd/license/license_generate.go diff --git a/licenses/license_header.go b/licenses/license_header.go new file mode 100644 index 00000000000..1512b742e49 --- /dev/null +++ b/licenses/license_header.go @@ -0,0 +1,58 @@ +// Licensed to Elasticsearch B.V. under one or more contributor +// license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright +// ownership. Elasticsearch B.V. licenses this file to you 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. + +// Code generated by beats/dev-tools/cmd/license/license_generate.go - DO NOT EDIT. + +package licenses + +import "fmt" + +var ASL2 = ` +// Licensed to Elasticsearch B.V. under one or more contributor +// license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright +// ownership. Elasticsearch B.V. licenses this file to you 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. +` + +var Elastic = ` +// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one +// or more contributor license agreements. Licensed under the Elastic License; +// you may not use this file except in compliance with the Elastic License. +` + +func Find(name string) (string, error) { + switch name { + + case "ASL2": + return ASL2, nil + case "Elastic": + return Elastic, nil + } + return "", fmt.Errorf("unknown license: %s", name) +} diff --git a/metricbeat/Makefile b/metricbeat/Makefile index cbf765d188d..0a4dc962a0e 100644 --- a/metricbeat/Makefile +++ b/metricbeat/Makefile @@ -77,5 +77,5 @@ test-module: python-env update metricbeat.test assets: go run ${ES_BEATS}/metricbeat/scripts/assets/assets.go ${ES_BEATS}/metricbeat/module mkdir -p include/fields - go run ${ES_BEATS}/dev-tools/cmd/asset/asset.go -pkg include -in ${ES_BEATS}/metricbeat/_meta/fields.common.yml -out include/fields.go $(BEAT_NAME) - go run ${ES_BEATS}/libbeat/scripts/cmd/global_fields/main.go -es_beats_path ${ES_BEATS} -beat_path ${PWD} | go run ${ES_BEATS}/dev-tools/cmd/asset/asset.go -out ./include/fields/fields.go -pkg include ${ES_BEATS}/libbeat/fields.yml $(BEAT_NAME) + go run ${ES_BEATS}/dev-tools/cmd/asset/asset.go -license ${LICENSE} -pkg include -in ${ES_BEATS}/metricbeat/_meta/fields.common.yml -out include/fields.go $(BEAT_NAME) + go run ${ES_BEATS}/libbeat/scripts/cmd/global_fields/main.go -es_beats_path ${ES_BEATS} -beat_path ${PWD} | go run ${ES_BEATS}/dev-tools/cmd/asset/asset.go -license ${LICENSE} -out ./include/fields/fields.go -pkg include ${ES_BEATS}/libbeat/fields.yml $(BEAT_NAME) diff --git a/metricbeat/scripts/assets/assets.go b/metricbeat/scripts/assets/assets.go index 1cb0a44dd99..e103eb70707 100644 --- a/metricbeat/scripts/assets/assets.go +++ b/metricbeat/scripts/assets/assets.go @@ -28,6 +28,7 @@ import ( "github.com/elastic/beats/libbeat/asset" "github.com/elastic/beats/libbeat/generator/fields" + "github.com/elastic/beats/licenses" ) func main() { @@ -69,6 +70,7 @@ func main() { var buf bytes.Buffer asset.Template.Execute(&buf, asset.Data{ + License: licenses.ASL2, Beat: "metricbeat", Name: module, Data: encData,