Skip to content
This repository has been archived by the owner on Dec 15, 2020. It is now read-only.

Commit

Permalink
Allow import of github.com/kolide/fleet (#2213)
Browse files Browse the repository at this point in the history
Previously a Go package attempting to import Fleet packages would run
into an error like "server/kolide/emails.go:93:23: undefined: Asset".

This commit refactors bindata asset handling to allow importing Fleet as
a library without changing the typical developer experience.
  • Loading branch information
zwass authored Mar 30, 2020
1 parent d3849d8 commit 45f6a74
Show file tree
Hide file tree
Showing 15 changed files with 179 additions and 152 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ assets/*@*.woff2
assets/*@*.ttf
frontend/templates/react.tmpl
bindata.go
server/bindata/generated.go
*.cover
*.test
*.log
Expand Down
34 changes: 16 additions & 18 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,10 @@ endif
build: fleet fleetctl

fleet: .prefix .pre-build .pre-fleet
go build -i -o build/${OUTPUT} -ldflags ${KIT_VERSION} ./cmd/fleet
go build -tags full -i -o build/${OUTPUT} -ldflags ${KIT_VERSION} ./cmd/fleet

fleetctl: .prefix .pre-build .pre-fleetctl
go build -i -o build/fleetctl -ldflags ${KIT_VERSION} ./cmd/fleetctl
go build -tags full -i -o build/fleetctl -ldflags ${KIT_VERSION} ./cmd/fleetctl

lint-js:
yarn run eslint frontend --ext .js,.jsx
Expand All @@ -127,10 +127,10 @@ lint-go:
lint: lint-go lint-js lint-scss lint-ts

test-go:
go test ./...
go test -tags full ./...

analyze-go:
go test -race -cover ./...
go test -tags full -race -cover ./...

test-js: export NODE_PATH = ./frontend
test-js:
Expand All @@ -148,20 +148,18 @@ generate-js: .prefix
NODE_ENV=production webpack --progress --colors

generate-go: .prefix
go-bindata -pkg=service \
-o=server/service/bindata.go \
frontend/templates/ assets/...
go-bindata -pkg=kolide -o=server/kolide/bindata.go server/mail/templates
go-bindata -pkg=bindata -tags full \
-o=server/bindata/generated.go \
frontend/templates/ assets/... server/mail/templates

# we first generate the webpack bundle so that bindata knows to watch the
# output bundle file. then, generate debug bindata source file. finally, we
# run webpack in watch mode to continuously re-generate the bundle
generate-dev: .prefix
NODE_ENV=development webpack --progress --colors
go-bindata -debug -pkg=service \
-o=server/service/bindata.go \
frontend/templates/ assets/...
go-bindata -pkg=kolide -o=server/kolide/bindata.go server/mail/templates
go-bindata -debug -pkg=bindata -tags full \
-o=server/bindata/generated.go \
frontend/templates/ assets/... server/mail/templates
NODE_ENV=development webpack --progress --colors --watch

deps: deps-js deps-go
Expand Down Expand Up @@ -219,14 +217,14 @@ demo-restore:
mkdir -p build/binary-bundle/darwin

xp-fleet: .pre-binary-bundle .pre-fleet generate
GOOS=linux go build -i -o build/binary-bundle/linux/fleet -ldflags ${KIT_VERSION} ./cmd/fleet
GOOS=darwin go build -i -o build/binary-bundle/darwin/fleet -ldflags ${KIT_VERSION} ./cmd/fleet
GOOS=windows go build -i -o build/binary-bundle/windows/fleet.exe -ldflags ${KIT_VERSION} ./cmd/fleet
GOOS=linux go build -tags full -i -o build/binary-bundle/linux/fleet -ldflags ${KIT_VERSION} ./cmd/fleet
GOOS=darwin go build -tags full -i -o build/binary-bundle/darwin/fleet -ldflags ${KIT_VERSION} ./cmd/fleet
GOOS=windows go build -tags full -i -o build/binary-bundle/windows/fleet.exe -ldflags ${KIT_VERSION} ./cmd/fleet

xp-fleetctl: .pre-binary-bundle .pre-fleetctl generate
GOOS=linux go build -i -o build/binary-bundle/linux/fleetctl -ldflags ${KIT_VERSION} ./cmd/fleetctl
GOOS=darwin go build -i -o build/binary-bundle/darwin/fleetctl -ldflags ${KIT_VERSION} ./cmd/fleetctl
GOOS=windows go build -i -o build/binary-bundle/windows/fleetctl.exe -ldflags ${KIT_VERSION} ./cmd/fleetctl
GOOS=linux go build -tags full -i -o build/binary-bundle/linux/fleetctl -ldflags ${KIT_VERSION} ./cmd/fleetctl
GOOS=darwin go build -tags full -i -o build/binary-bundle/darwin/fleetctl -ldflags ${KIT_VERSION} ./cmd/fleetctl
GOOS=windows go build -tags full -i -o build/binary-bundle/windows/fleetctl.exe -ldflags ${KIT_VERSION} ./cmd/fleetctl

binary-bundle: xp-fleet xp-fleetctl
cd build/binary-bundle && zip -r "fleet_${VERSION}.zip" darwin/ linux/ windows/
Expand Down
21 changes: 21 additions & 0 deletions server/bindata/placeholder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// +build !full

package bindata

import "os"

// This file contains placeholders for the asset functions that would normally
// allow Fleet to retrieve assets and templates. Providing these placeholders
// allows Fleet packages to be included as a library with `go get`.

func Asset(name string) ([]byte, error) {
panic("Assets may not be used when running Fleet as a library")
}

func AssetDir(name string) ([]string, error) {
panic("Assets may not be used when running Fleet as a library")
}

func AssetInfo(name string) (os.FileInfo, error) {
panic("Assets may not be used when running Fleet as a library")
}
59 changes: 0 additions & 59 deletions server/kolide/emails.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package kolide

import (
"bytes"
"html/template"
"time"
)

Expand Down Expand Up @@ -45,60 +43,3 @@ type PasswordResetRequest struct {
UserID uint `db:"user_id"`
Token string
}

// SMTPTestMailer is used to build an email message that will be used as
// a test message when testing SMTP configuration
type SMTPTestMailer struct {
BaseURL template.URL
AssetURL template.URL
}

func (m *SMTPTestMailer) Message() ([]byte, error) {
t, err := getTemplate("server/mail/templates/smtp_setup.html")
if err != nil {
return nil, err
}

var msg bytes.Buffer
if err = t.Execute(&msg, m); err != nil {
return nil, err
}

return msg.Bytes(), nil
}

type PasswordResetMailer struct {
// Base URL to use for Fleet endpoints
BaseURL template.URL
// URL for loading image assets
AssetURL template.URL
// Token password reset token
Token string
}

func (r PasswordResetMailer) Message() ([]byte, error) {
t, err := getTemplate("server/mail/templates/password_reset.html")
if err != nil {
return nil, err
}

var msg bytes.Buffer
if err = t.Execute(&msg, r); err != nil {
return nil, err
}
return msg.Bytes(), nil
}

func getTemplate(templatePath string) (*template.Template, error) {
templateData, err := Asset(templatePath)
if err != nil {
return nil, err
}

t, err := template.New("email_template").Parse(string(templateData))
if err != nil {
return nil, err
}

return t, nil
}
19 changes: 0 additions & 19 deletions server/kolide/emails_test.go

This file was deleted.

24 changes: 0 additions & 24 deletions server/kolide/invites.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package kolide

import (
"bytes"
"context"
"html/template"
)

// InviteStore contains the methods for
Expand Down Expand Up @@ -71,25 +69,3 @@ type Invite struct {
Token string `json:"-"`
SSOEnabled bool `json:"sso_enabled" db:"sso_enabled"`
}

// InviteMailer is used to build an email template for the invite email.
type InviteMailer struct {
*Invite
BaseURL template.URL
AssetURL template.URL
InvitedByUsername string
OrgName string
}

func (i *InviteMailer) Message() ([]byte, error) {
t, err := getTemplate("server/mail/templates/invite_token.html")
if err != nil {
return nil, err
}

var msg bytes.Buffer
if err = t.Execute(&msg, i); err != nil {
return nil, err
}
return msg.Bytes(), nil
}
21 changes: 0 additions & 21 deletions server/kolide/users.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package kolide

import (
"bytes"
"context"
"crypto/rand"
"encoding/base64"
"fmt"
"html/template"

"golang.org/x/crypto/bcrypt"
)
Expand Down Expand Up @@ -192,22 +190,3 @@ func falseIfNil(b *bool) bool {
}
return *b
}

type ChangeEmailMailer struct {
BaseURL template.URL
AssetURL template.URL
Token string
}

func (cem *ChangeEmailMailer) Message() ([]byte, error) {
t, err := getTemplate("server/mail/templates/change_email_confirmation.html")
if err != nil {
return nil, err
}
var msg bytes.Buffer
err = t.Execute(&msg, cem)
if err != nil {
return nil, err
}
return msg.Bytes(), nil
}
30 changes: 30 additions & 0 deletions server/mail/invite.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package mail

import (
"bytes"
"html/template"

"github.com/kolide/fleet/server/kolide"
)

// InviteMailer is used to build an email template for the invite email.
type InviteMailer struct {
*kolide.Invite
BaseURL template.URL
AssetURL template.URL
InvitedByUsername string
OrgName string
}

func (i *InviteMailer) Message() ([]byte, error) {
t, err := getTemplate("server/mail/templates/invite_token.html")
if err != nil {
return nil, err
}

var msg bytes.Buffer
if err = t.Execute(&msg, i); err != nil {
return nil, err
}
return msg.Bytes(), nil
}
38 changes: 38 additions & 0 deletions server/mail/mail.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@
package mail

import (
"bytes"
"crypto/tls"
"fmt"
"html/template"
"net"
"net/smtp"
"strings"
"time"

"github.com/kolide/fleet/server/bindata"
"github.com/kolide/fleet/server/kolide"
"github.com/pkg/errors"
)
Expand Down Expand Up @@ -231,3 +234,38 @@ func dialTimeout(addr string) (client *smtp.Client, err error) {

return client, nil
}

// SMTPTestMailer is used to build an email message that will be used as
// a test message when testing SMTP configuration
type SMTPTestMailer struct {
BaseURL template.URL
AssetURL template.URL
}

func (m *SMTPTestMailer) Message() ([]byte, error) {
t, err := getTemplate("server/mail/templates/smtp_setup.html")
if err != nil {
return nil, err
}

var msg bytes.Buffer
if err = t.Execute(&msg, m); err != nil {
return nil, err
}

return msg.Bytes(), nil
}

func getTemplate(templatePath string) (*template.Template, error) {
templateData, err := bindata.Asset(templatePath)
if err != nil {
return nil, err
}

t, err := template.New("email_template").Parse(string(templateData))
if err != nil {
return nil, err
}

return t, nil
}
Loading

0 comments on commit 45f6a74

Please sign in to comment.