-
Notifications
You must be signed in to change notification settings - Fork 28
Refactor manager.go for improved safety and maintainability #1082
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,7 +52,6 @@ func NewManager(ctx context.Context, brokersConfPath string, configuredBrokers [ | |
}() | ||
|
||
// Connect to the system bus | ||
// Don't call dbus.SystemBus which caches globally system dbus (issues in tests) | ||
bus, err := dbus.ConnectSystemBus() | ||
if err != nil { | ||
return m, err | ||
|
@@ -119,6 +118,7 @@ func NewManager(ctx context.Context, brokersConfPath string, configuredBrokers [ | |
|
||
// AvailableBrokers returns currently loaded and available brokers in preference order. | ||
func (m *Manager) AvailableBrokers() (r []*Broker) { | ||
r = make([]*Broker, 0, len(m.brokersOrder)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This change is fine, I see that it can avoid unnecessary allocations when appending the brokers. However, that's not explained in the commit message. Please create a separate commit with this change and explain in the commit message why it is useful. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Optimize AvailableBrokers to preallocate slice capacity Preallocate the slice in AvailableBrokers with len(m.brokersOrder) capacity:
This avoids repeated slice reallocations when appending brokers, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. True, we don't have that many though... But well still fine. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This would be a good commit message. Again, please create a separate commit with this change. |
||
for _, id := range m.brokersOrder { | ||
r = append(r, m.brokers[id]) | ||
} | ||
|
@@ -145,7 +145,7 @@ func (m *Manager) SetDefaultBrokerForUser(brokerID, username string) error { | |
func (m *Manager) BrokerForUser(username string) (broker *Broker) { | ||
m.usersToBrokerMu.RLock() | ||
defer m.usersToBrokerMu.RUnlock() | ||
return m.usersToBroker[username] | ||
return m.usersToBroker[strings.ToLower(username)] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm a bit undecided if we want this change or not. We do use lowercase usernames in authd, so this username must indeed be lowercase. However, we have so many layers of functions which take a username as an argument that it's a bit inefficient to convert it lowercase in each function. Here we have these layers: sequenceDiagram
box PAM
participant A as authd-pam
end
box authd
participant B as pam.Service
participant C as brokers.Manager
end
Note over A,B: Communication via gRPC
A->>B: GetPreviousBroker
B->>C: BrokerForUser
C-->>B: Return broker
B-->>A: Response
If the sequenceDiagram
box PAM
participant A as authd-pam
end
box authd
participant B as pam.Service
participant C as users.Manager
participant D as db.Manager
end
Note over A,B: Communication via gRPC
A->>B: GetPreviousBroker
B->>C: BrokerForUser
C->>D: UserByName
D-->>C: UserRow.BrokerID
C-->>B: Return BrokerID
B-->>A: Response
We currently call Looking at it like this, it's my impression that it would make most sense to just convert the usernames to lowercase once in the gRPC methods, and in the other layers ( Note that the same applies to all other methods of What do you think, @3v1n0? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I was noticing the change too and I was wondering whether is needed... Ideally we should do it in any public APIs though at this point... |
||
} | ||
|
||
// BrokerFromSessionID returns broker currently in use for a given transaction sessionID. | ||
|
@@ -160,49 +160,49 @@ func (m *Manager) BrokerFromSessionID(id string) (broker *Broker, err error) { | |
|
||
broker, exists := m.transactionsToBroker[id] | ||
if !exists { | ||
return nil, fmt.Errorf("no broker found for session %q", id) | ||
return nil, fmt.Errorf("no broker found for session ID %q", id) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We use "session %q" all over the place in error and debug messages, so for consistency we should not change it here. |
||
} | ||
|
||
return broker, nil | ||
} | ||
|
||
// NewSession create a new session for the broker and store the sesssionID on the manager. | ||
func (m *Manager) NewSession(brokerID, username, lang, mode string) (sessionID string, encryptionKey string, err error) { | ||
// NewSession create a new session for the broker and store the sessionID on the manager. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please create a separate commit with this change. The commit message can simply be "Fix typo". There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see you pushed a commit with the commit message "Fix typo". However, it doesn't contain this change but adds a comment |
||
// Fix typo | ||
func (m *Manager) NewSession(ctx context.Context, brokerID, username, lang, mode string) (sessionID string, encryptionKey string, err error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's not add unused context parameters to functions. The |
||
broker, err := m.brokerFromID(brokerID) | ||
if err != nil { | ||
return "", "", fmt.Errorf("invalid broker: %v", err) | ||
} | ||
|
||
sessionID, encryptionKey, err = broker.newSession(context.Background(), username, lang, mode) | ||
sessionID, encryptionKey, err = broker.newSession(ctx, username, lang, mode) | ||
if err != nil { | ||
return "", "", err | ||
} | ||
|
||
m.transactionsToBrokerMu.Lock() | ||
defer m.transactionsToBrokerMu.Unlock() | ||
log.Debugf(context.Background(), "%s: New %s session for %q", | ||
sessionID, mode, username) | ||
log.Debugf(ctx, "%s: New %s session for %q", sessionID, mode, username) | ||
m.transactionsToBroker[sessionID] = broker | ||
return sessionID, encryptionKey, nil | ||
} | ||
|
||
// EndSession signals the end of the session to the broker associated with the sessionID and then removes the | ||
// session -> broker mapping. | ||
func (m *Manager) EndSession(sessionID string) error { | ||
b, err := m.BrokerFromSessionID(sessionID) | ||
if err != nil { | ||
return err | ||
func (m *Manager) EndSession(ctx context.Context, sessionID string) error { | ||
m.transactionsToBrokerMu.Lock() | ||
broker, exists := m.transactionsToBroker[sessionID] | ||
if !exists { | ||
m.transactionsToBrokerMu.Unlock() | ||
return fmt.Errorf("no broker found for session ID %q", sessionID) | ||
} | ||
Comment on lines
189
to
197
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why should we change the order of deleting session mapping and calling |
||
delete(m.transactionsToBroker, sessionID) | ||
m.transactionsToBrokerMu.Unlock() | ||
|
||
if err = b.endSession(context.Background(), sessionID); err != nil { | ||
if err := broker.endSession(ctx, sessionID); err != nil { | ||
return err | ||
} | ||
|
||
m.transactionsToBrokerMu.Lock() | ||
log.Debugf(context.Background(), "%s: End session %q", | ||
sessionID, m.transactionsToBroker[sessionID].Name) | ||
delete(m.transactionsToBroker, sessionID) | ||
m.transactionsToBrokerMu.Unlock() | ||
log.Debugf(ctx, "%s: Ended session", sessionID) | ||
return nil | ||
} | ||
|
||
|
@@ -216,8 +216,9 @@ func (m *Manager) BrokerExists(brokerID string) bool { | |
func (m *Manager) brokerFromID(id string) (broker *Broker, err error) { | ||
broker, exists := m.brokers[id] | ||
if !exists { | ||
return nil, fmt.Errorf("no broker found matching %q", id) | ||
} | ||
// Fix error message to include ID in broker lookup | ||
return nil, fmt.Errorf("no broker found matching ID %q", id) | ||
|
||
} | ||
return broker, nil | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't remove this comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
okey thank You