Skip to content

Commit

Permalink
feat (authmethod/password): add additional telmemetry for passwd acct…
Browse files Browse the repository at this point in the history
… authen (#3958)

(cherry picked from commit 75cef86)
  • Loading branch information
jimlambrt committed Oct 30, 2023
1 parent 228f146 commit e6583e5
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
7 changes: 7 additions & 0 deletions internal/daemon/controller/handlers/authmethods/password.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/hashicorp/boundary/internal/daemon/controller/auth"
"github.com/hashicorp/boundary/internal/daemon/controller/handlers"
"github.com/hashicorp/boundary/internal/errors"
"github.com/hashicorp/boundary/internal/event"
pbs "github.com/hashicorp/boundary/internal/gen/controller/api/services"
"github.com/hashicorp/boundary/internal/types/action"
pb "github.com/hashicorp/boundary/sdk/pbs/controller/api/resources/authmethods"
Expand Down Expand Up @@ -100,6 +101,7 @@ func (s Service) authenticatePassword(ctx context.Context, req *pbs.Authenticate
}

func (s Service) authenticateWithPwRepo(ctx context.Context, scopeId, authMethodId, loginName, pw string) (*pba.AuthToken, error) {
const op = "authmethods.(Service).authenticateWithPwRepo"
iamRepo, err := s.iamRepoFn()
if err != nil {
return nil, err
Expand Down Expand Up @@ -130,6 +132,11 @@ func (s Service) authenticateWithPwRepo(ctx context.Context, scopeId, authMethod
return nil, err
}

if err := event.WriteObservation(ctx, op, event.WithDetails("user_id", u.GetPublicId(), "auth_token_start",
tok.GetCreateTime(), "auth_token_end", tok.GetExpirationTime())); err != nil {
return nil, errors.Wrap(ctx, err, op, errors.WithMsg("Unable to write observation event for authenticate method"))
}

return s.ConvertInternalAuthTokenToApiAuthToken(
ctx,
tok,
Expand Down
32 changes: 32 additions & 0 deletions internal/daemon/controller/handlers/authmethods/password_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ package authmethods_test

import (
"context"
"encoding/json"
"os"
"strings"
"sync"
"testing"

"github.com/google/go-cmp/cmp"
Expand All @@ -19,12 +22,15 @@ import (
"github.com/hashicorp/boundary/internal/daemon/controller/handlers/authmethods"
"github.com/hashicorp/boundary/internal/db"
"github.com/hashicorp/boundary/internal/errors"
"github.com/hashicorp/boundary/internal/event"
pbs "github.com/hashicorp/boundary/internal/gen/controller/api/services"
"github.com/hashicorp/boundary/internal/iam"
"github.com/hashicorp/boundary/internal/kms"
"github.com/hashicorp/boundary/internal/types/scope"
pb "github.com/hashicorp/boundary/sdk/pbs/controller/api/resources/authmethods"
scopepb "github.com/hashicorp/boundary/sdk/pbs/controller/api/resources/scopes"
"github.com/hashicorp/eventlogger/formatter_filters/cloudevents"
"github.com/hashicorp/go-hclog"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"google.golang.org/genproto/protobuf/field_mask"
Expand Down Expand Up @@ -508,6 +514,18 @@ func TestAuthenticate_Password(t *testing.T) {
require.NoError(t, err)
require.NotNil(t, acct)

c := event.TestEventerConfig(t, "Test_StartAuth_to_Callback", event.TestWithObservationSink(t))
testLock := &sync.Mutex{}
testLogger := hclog.New(&hclog.LoggerOptions{
Mutex: testLock,
Name: "test",
})
require.NoError(t, event.InitSysEventer(testLogger, testLock, "use-Test_Authenticate", event.WithEventerConfig(&c.EventerConfig)))
sinkFileName := c.ObservationEvents.Name()
t.Cleanup(func() {
require.NoError(t, os.Remove(sinkFileName))
})

cases := []struct {
name string
request *pbs.AuthenticateRequest
Expand Down Expand Up @@ -648,6 +666,20 @@ func TestAuthenticate_Password(t *testing.T) {
assert.Equal(acct.GetPublicId(), aToken.GetAccountId())
assert.Equal(am.GetPublicId(), aToken.GetAuthMethodId())
assert.Equal(tc.wantType, resp.GetType())

defer func() { _ = os.WriteFile(sinkFileName, nil, 0o666) }()
b, err := os.ReadFile(sinkFileName)
require.NoError(err)
gotRes := &cloudevents.Event{}
err = json.Unmarshal(b, gotRes)
require.NoErrorf(err, "json: %s", string(b))
details, ok := gotRes.Data.(map[string]any)["details"]
require.True(ok)
for _, key := range details.([]any) {
assert.Contains(key.(map[string]any)["payload"], "user_id")
assert.Contains(key.(map[string]any)["payload"], "auth_token_start")
assert.Contains(key.(map[string]any)["payload"], "auth_token_end")
}
})
}
}
Expand Down

0 comments on commit e6583e5

Please sign in to comment.