Skip to content
This repository has been archived by the owner on Mar 11, 2021. It is now read-only.

Support notifications to users with private email ( using fabric8 service account ) #54

Merged
merged 19 commits into from
Jan 24, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ tool/cli/
migration/sqlbindata.go
template/bindata.go
wit/api
auth/api

# Ignore artifacts directory
bin/
Expand Down
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ clean-generated:
-rm -rf ./swagger/
-rm -f ./migration/sqlbindata.go
-rm -f ./template/bindata.go
-rm -rf auth

CLEAN_TARGETS += clean-vendor
.PHONY: clean-vendor
Expand All @@ -211,6 +212,7 @@ app/controllers.go: $(DESIGNS) $(GOAGEN_BIN) $(VENDOR_DIR)
$(GOAGEN_BIN) app -d ${PACKAGE_NAME}/${DESIGN_DIR}
$(GOAGEN_BIN) controller -d ${PACKAGE_NAME}/${DESIGN_DIR} -o controller/ --pkg controller --app-pkg app
$(GOAGEN_BIN) client -d github.com/fabric8-services/fabric8-wit/design --notool --pkg api -o wit
$(GOAGEN_BIN) client -d github.com/fabric8-services/fabric8-auth/design --notool --pkg api -o auth

.PHONY: migrate-database
## Compiles the server and runs the database migration with it
Expand Down
65 changes: 65 additions & 0 deletions auth/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package auth

import (
"context"
"net/http"
"net/url"

"fmt"

"github.com/fabric8-services/fabric8-notification/auth/api"
"github.com/fabric8-services/fabric8-wit/goasupport"
goaclient "github.com/goadesign/goa/client"
"github.com/goadesign/goa/uuid"
"github.com/gregjones/httpcache"
)

func NewCachedClient(hostURL string) (*api.Client, error) {

u, err := url.Parse(hostURL)
if err != nil {
return nil, err
}

tp := httpcache.NewMemoryCacheTransport()
client := http.Client{Transport: tp}

c := api.New(goaclient.HTTPClientDoer(&client))
c.Host = u.Host
c.Scheme = u.Scheme
return c, nil
}

func GetUser(ctx context.Context, client *api.Client, uID uuid.UUID) (*api.User, error) {
resp, err := client.ShowUsers(goasupport.ForwardContextRequestID(ctx), api.ShowUsersPath(uID.String()), nil, nil)
if resp != nil {
defer resp.Body.Close()
}

if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("non %v status code for %v, returned %v", http.StatusOK, "GET user", resp.StatusCode)
}

if err != nil {
return nil, err
}
return client.DecodeUser(resp)
}

func GetSpaceCollaborators(ctx context.Context, client *api.Client, spaceID uuid.UUID) (*api.UserList, error) {
pageLimit := 100
pageOffset := "0"
resp, err := client.ListCollaborators(goasupport.ForwardContextRequestID(ctx), api.ListCollaboratorsPath(spaceID), &pageLimit, &pageOffset, nil, nil)
if resp != nil {
defer resp.Body.Close()
}

if resp.StatusCode != http.StatusOK {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

resp can be nil here. Check err first.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

37b3c4a , done.
had misunderstood your comment here initially.

return nil, fmt.Errorf("non %v status code for %v, returned %v", http.StatusOK, "GET collaborators", resp.StatusCode)
}

if err != nil {
return nil, err
}
return client.DecodeUserList(resp)
}
22 changes: 12 additions & 10 deletions collector/workitem.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"fmt"

jwt "github.com/dgrijalva/jwt-go"
"github.com/fabric8-services/fabric8-notification/auth"
authapi "github.com/fabric8-services/fabric8-notification/auth/api"
"github.com/fabric8-services/fabric8-notification/configuration"
"github.com/fabric8-services/fabric8-notification/wit"
"github.com/fabric8-services/fabric8-notification/wit/api"
Expand All @@ -13,23 +15,23 @@ import (
"github.com/goadesign/goa/uuid"
)

func NewCommentResolver(c *api.Client) ReceiverResolver {
func NewCommentResolver(authclient *authapi.Client, c *api.Client) ReceiverResolver {
return func(ctx context.Context, id string) ([]Receiver, map[string]interface{}, error) {
cID, err := uuid.FromString(id)
if err != nil {
return []Receiver{}, nil, fmt.Errorf("unable to lookup comment based on id %v", id)
}
return Comment(ctx, c, cID)
return Comment(ctx, authclient, c, cID)
}
}

func NewWorkItemResolver(c *api.Client) ReceiverResolver {
func NewWorkItemResolver(authclient *authapi.Client, c *api.Client) ReceiverResolver {
return func(ctx context.Context, id string) ([]Receiver, map[string]interface{}, error) {
wID, err := uuid.FromString(id)
if err != nil {
return []Receiver{}, nil, fmt.Errorf("unable to lookup Workitem based on id %v", id)
}
return WorkItem(ctx, c, wID)
return WorkItem(ctx, authclient, c, wID)
}
}

Expand All @@ -45,7 +47,7 @@ func ConfiguredVars(config *configuration.Data, resolver ReceiverResolver) Recei
}
}

func Comment(ctx context.Context, c *api.Client, cID uuid.UUID) ([]Receiver, map[string]interface{}, error) {
func Comment(ctx context.Context, authClient *authapi.Client, c *api.Client, cID uuid.UUID) ([]Receiver, map[string]interface{}, error) {
var values = map[string]interface{}{}
var errors []error
var users []uuid.UUID
Expand Down Expand Up @@ -121,7 +123,7 @@ func Comment(ctx context.Context, c *api.Client, cID uuid.UUID) ([]Receiver, map
values["actor"] = actor
}

sc, err := wit.GetSpaceCollaborators(ctx, c, spaceID)
sc, err := auth.GetSpaceCollaborators(ctx, authClient, spaceID)
if err != nil {
errors = append(errors, err)
}
Expand All @@ -140,7 +142,7 @@ func Comment(ctx context.Context, c *api.Client, cID uuid.UUID) ([]Receiver, map
return resolved, values, nil
}

func WorkItem(ctx context.Context, c *api.Client, wiID uuid.UUID) ([]Receiver, map[string]interface{}, error) {
func WorkItem(ctx context.Context, authclient *authapi.Client, c *api.Client, wiID uuid.UUID) ([]Receiver, map[string]interface{}, error) {
var values = map[string]interface{}{}
var errors []error
var users []uuid.UUID
Expand Down Expand Up @@ -201,7 +203,7 @@ func WorkItem(ctx context.Context, c *api.Client, wiID uuid.UUID) ([]Receiver, m
values["actor"] = actor
}

sc, err := wit.GetSpaceCollaborators(ctx, c, spaceID)
sc, err := auth.GetSpaceCollaborators(ctx, authclient, spaceID)
if err != nil {
errors = append(errors, err)
}
Expand All @@ -220,7 +222,7 @@ func WorkItem(ctx context.Context, c *api.Client, wiID uuid.UUID) ([]Receiver, m
return resolved, values, nil
}

func resolveAllUsers(ctx context.Context, c *api.Client, users []uuid.UUID, collaborators []*api.UserData) ([]Receiver, error) {
func resolveAllUsers(ctx context.Context, c *api.Client, users []uuid.UUID, collaborators []*authapi.UserData) ([]Receiver, error) {
var resolved []Receiver

for _, u := range users {
Expand Down Expand Up @@ -260,7 +262,7 @@ func resolveAllUsers(ctx context.Context, c *api.Client, users []uuid.UUID, coll
return resolved, nil
}

func collectSpaceCollaboratorUsers(cl *api.UserList) []uuid.UUID {
func collectSpaceCollaboratorUsers(cl *authapi.UserList) []uuid.UUID {
var users []uuid.UUID
for _, c := range cl.Data {
cID, err := uuid.FromString(*c.ID)
Expand Down
12 changes: 10 additions & 2 deletions configuration/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"strings"

log "github.com/Sirupsen/logrus"
log "github.com/sirupsen/logrus"
yaml "gopkg.in/yaml.v2"

"github.com/spf13/viper"
Expand All @@ -17,6 +17,7 @@ const (
varHTTPAddress = "http.address"
varDeveloperModeEnabled = "developer.mode.enabled"
varWITURL = "wit.url"
varAuthURL = "auth.url"
varMadrillAPIKey = "mandrill.apikey"
varKeycloakRealm = "keycloak.realm"
varKeycloakURL = "keycloak.url"
Expand Down Expand Up @@ -77,6 +78,7 @@ func (c *Data) setConfigDefaults() {
c.v.SetDefault(varHTTPAddress, "0.0.0.0:8080")

c.v.SetDefault(varWITURL, defaultWITURL)
c.v.SetDefault(varAuthURL, defaultAuthURL)

//-----
// Misc
Expand Down Expand Up @@ -107,6 +109,11 @@ func (c *Data) GetWITURL() string {
return c.v.GetString(varWITURL)
}

// GetAuthURL return the base Auth API URL
func (c *Data) GetAuthURL() string {
return c.v.GetString(varWITURL)
}

// GetServiceAccountID returns service account ID for the notification service.
// This will be used by the notification service to request for a service account token
// from the Auth service.
Expand Down Expand Up @@ -177,7 +184,8 @@ func (c *Data) Validate() error {
}

const (
defaultWITURL = "https://api.openshift.io/"
defaultWITURL = "https://api.openshift.io/"
defaultAuthURL = "https://auth.openshift.io/"

// Auth-related defaults
defaultKeycloakURL = "https://sso.prod-preview.openshift.io"
Expand Down
29 changes: 21 additions & 8 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions glide.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package: github.com/fabric8-services/fabric8-notification
license: Apache-2.0
import:
- package: github.com/fabric8-services/fabric8-wit
version: 58db841e76891f1251bdec86908c61360f11d375
version: ff1746954f6c1a89dc2c35b72731e19b63d842d2
subpackages:
- log
- errors
Expand All @@ -11,6 +11,10 @@ import:
- jsonapi
- design
- goasupport
- package: github.com/fabric8-services/fabric8-auth
version: f67317193c3108895f853f80f9330c1a65b11004
subpackages:
- design
- package: github.com/gregjones/httpcache
- package: github.com/mattbaird/gochimp
- package: github.com/goadesign/goa
Expand All @@ -34,7 +38,7 @@ import:
- package: github.com/zach-klippenstein/goregen
- package: github.com/spf13/cobra
- package: github.com/spf13/viper
- package: github.com/Sirupsen/logrus
- package: github.com/sirupsen/logrus
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

- package: github.com/lib/pq
- package: gopkg.in/yaml.v2
- package: github.com/jteeuwen/go-bindata
Expand Down
19 changes: 14 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"net/http"

"github.com/fabric8-services/fabric8-notification/app"
"github.com/fabric8-services/fabric8-notification/auth"
"github.com/fabric8-services/fabric8-notification/collector"
"github.com/fabric8-services/fabric8-notification/configuration"
"github.com/fabric8-services/fabric8-notification/controller"
Expand All @@ -14,13 +15,13 @@ import (
"github.com/fabric8-services/fabric8-notification/validator"
"github.com/fabric8-services/fabric8-notification/wit"

"github.com/Sirupsen/logrus"
witmiddleware "github.com/fabric8-services/fabric8-wit/goamiddleware"
"github.com/fabric8-services/fabric8-wit/log"
"github.com/goadesign/goa"
"github.com/goadesign/goa/middleware"
"github.com/goadesign/goa/middleware/gzip"
goajwt "github.com/goadesign/goa/middleware/security/jwt"
"github.com/sirupsen/logrus"

goalogrus "github.com/goadesign/goa/logging/logrus"
)
Expand Down Expand Up @@ -65,6 +66,14 @@ func main() {
}, "Could not create WIT client")
}

authClient, err := auth.NewCachedClient(config.GetWITURL())
if err != nil {
log.Panic(nil, map[string]interface{}{
"url": config.GetWITURL(),
"err": err,
}, "Could not create WIT client")
}

sender, err := email.NewMandrillSender(config.GetMadrillAPIKey())
if err != nil {
log.Panic(nil, map[string]interface{}{
Expand All @@ -75,10 +84,10 @@ func main() {
notifier := email.NewAsyncWorkerNotifier(sender, 1)

resolvers := &collector.LocalRegistry{}
resolvers.Register("workitem.create", collector.ConfiguredVars(config, collector.NewWorkItemResolver(witClient)), nil)
resolvers.Register("workitem.update", collector.ConfiguredVars(config, collector.NewWorkItemResolver(witClient)), nil)
resolvers.Register("comment.create", collector.ConfiguredVars(config, collector.NewCommentResolver(witClient)), nil)
resolvers.Register("comment.update", collector.ConfiguredVars(config, collector.NewCommentResolver(witClient)), nil)
resolvers.Register("workitem.create", collector.ConfiguredVars(config, collector.NewWorkItemResolver(authClient, witClient)), nil)
resolvers.Register("workitem.update", collector.ConfiguredVars(config, collector.NewWorkItemResolver(authClient, witClient)), nil)
resolvers.Register("comment.create", collector.ConfiguredVars(config, collector.NewCommentResolver(authClient, witClient)), nil)
resolvers.Register("comment.update", collector.ConfiguredVars(config, collector.NewCommentResolver(authClient, witClient)), nil)
resolvers.Register("user.email.update", collector.ConfiguredVars(config, collector.NewUserResolver(witClient)), validator.ValidateUser)

typeRegistry := &template.AssetRegistry{}
Expand Down
Loading