(ctx context.Context)
| 13 | ) |
| 14 | |
| 15 | func (d *Driver) getInstanceRoles(ctx context.Context) []*storepb.InstanceRole { |
| 16 | var instanceRoles []*storepb.InstanceRole |
| 17 | var users []string |
| 18 | var err error |
| 19 | users, err = d.getUsersFromMySQLUser(ctx) |
| 20 | if err != nil { |
| 21 | slog.Info("failed to get users", log.BBError(err)) |
| 22 | users, err = d.getUsersFromUserAttributes(ctx) |
| 23 | if err != nil { |
| 24 | slog.Info("failed to get users", log.BBError(err)) |
| 25 | return nil |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | // Uses single quote instead of backtick to escape because this is a string |
| 30 | // instead of table (which should use backtick instead). MySQL actually works |
| 31 | // in both ways. On the other hand, some other MySQL compatible engines might not (OceanBase in this case). |
| 32 | for _, name := range users { |
| 33 | grantList, err := d.getGrantFromUser(ctx, name) |
| 34 | if err != nil { |
| 35 | slog.Info("failed to get grants for user", slog.String("user", name), log.BBError(err)) |
| 36 | continue |
| 37 | } |
| 38 | attribute := strings.Join(grantList, "\n") |
| 39 | instanceRoles = append(instanceRoles, &storepb.InstanceRole{ |
| 40 | Name: name, |
| 41 | Attribute: &attribute, |
| 42 | }) |
| 43 | } |
| 44 | return instanceRoles |
| 45 | } |
| 46 | |
| 47 | // getGrantFromUser reads grants for user with format "'<user>'@'<host>'". |
| 48 | func (d *Driver) getGrantFromUser(ctx context.Context, name string) ([]string, error) { |
no test coverage detected