|
| 1 | +package db |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + |
| 7 | + "github.com/mongodb-forks/digest" |
| 8 | + "go.mongodb.org/atlas/mongodbatlas" |
| 9 | +) |
| 10 | + |
| 11 | +type AtlasRepository struct { |
| 12 | + atlas *mongodbatlas.Client |
| 13 | + groupId string |
| 14 | +} |
| 15 | + |
| 16 | +func NewAtlasRepository(ctx context.Context, groupId, publicKey, privateKey string) (Handler, error) { |
| 17 | + t := digest.NewTransport(publicKey, privateKey) |
| 18 | + tc, err := t.Client() |
| 19 | + if err != nil { |
| 20 | + return nil, err |
| 21 | + } |
| 22 | + |
| 23 | + return &AtlasRepository{ |
| 24 | + groupId: groupId, |
| 25 | + atlas: mongodbatlas.NewClient(tc), |
| 26 | + }, nil |
| 27 | +} |
| 28 | + |
| 29 | +func (m *AtlasRepository) Close(ctx context.Context) error { |
| 30 | + return nil |
| 31 | +} |
| 32 | + |
| 33 | +// CreateDatabaseIfNotExists is a dummy to apply to fulfill the contract, |
| 34 | +// we don't need to create the database on Atlas |
| 35 | +func (m *AtlasRepository) CreateDatabaseIfNotExists(ctx context.Context, database string) error { |
| 36 | + return nil |
| 37 | +} |
| 38 | + |
| 39 | +func (m *AtlasRepository) SetupUser(ctx context.Context, database string, username string, password string, roles Roles) error { |
| 40 | + doesUserExist, err := m.doesUserExist(ctx, database, username) |
| 41 | + if err != nil { |
| 42 | + return err |
| 43 | + } |
| 44 | + |
| 45 | + if !doesUserExist { |
| 46 | + if err := m.createUser(context.Background(), database, username, password, roles); err != nil { |
| 47 | + return err |
| 48 | + } |
| 49 | + if doesUserExistNow, err := m.doesUserExist(ctx, database, username); err != nil { |
| 50 | + return err |
| 51 | + } else if !doesUserExistNow { |
| 52 | + return errors.New("user doesn't exist after create") |
| 53 | + } |
| 54 | + } else { |
| 55 | + if err := m.updateUserPasswordAndRoles(ctx, database, username, password, roles); err != nil { |
| 56 | + return err |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + return nil |
| 61 | +} |
| 62 | + |
| 63 | +func (m *AtlasRepository) DropUser(ctx context.Context, database string, username string) error { |
| 64 | + //not implemented |
| 65 | + return errors.New("not yet supported") |
| 66 | +} |
| 67 | + |
| 68 | +func (m *AtlasRepository) EnableExtension(ctx context.Context, name string) error { |
| 69 | + // NOOP |
| 70 | + return nil |
| 71 | +} |
| 72 | + |
| 73 | +func (m *AtlasRepository) doesUserExist(ctx context.Context, database string, username string) (bool, error) { |
| 74 | + _, _, err := m.atlas.DatabaseUsers.Get(ctx, database, m.groupId, username) |
| 75 | + if err != nil { |
| 76 | + return false, nil |
| 77 | + } |
| 78 | + |
| 79 | + return true, err |
| 80 | +} |
| 81 | + |
| 82 | +func (m *AtlasRepository) getRoles(database string, roles Roles) []mongodbatlas.Role { |
| 83 | + // by default, assign readWrite role (backward compatibility) |
| 84 | + if len(roles) == 0 { |
| 85 | + return []mongodbatlas.Role{{ |
| 86 | + RoleName: "readWrite", |
| 87 | + DatabaseName: database, |
| 88 | + }} |
| 89 | + } |
| 90 | + |
| 91 | + rs := make([]mongodbatlas.Role, 0) |
| 92 | + for _, r := range roles { |
| 93 | + db := r.DB |
| 94 | + if db == "" { |
| 95 | + db = database |
| 96 | + } |
| 97 | + |
| 98 | + rs = append(rs, mongodbatlas.Role{ |
| 99 | + RoleName: r.Name, |
| 100 | + DatabaseName: db, |
| 101 | + }) |
| 102 | + } |
| 103 | + |
| 104 | + return rs |
| 105 | +} |
| 106 | + |
| 107 | +func (m *AtlasRepository) createUser(ctx context.Context, database string, username string, password string, roles Roles) error { |
| 108 | + user := &mongodbatlas.DatabaseUser{ |
| 109 | + Username: username, |
| 110 | + Password: password, |
| 111 | + DatabaseName: database, |
| 112 | + Roles: m.getRoles(database, roles), |
| 113 | + } |
| 114 | + |
| 115 | + _, _, err := m.atlas.DatabaseUsers.Create(ctx, m.groupId, user) |
| 116 | + if err != nil { |
| 117 | + return err |
| 118 | + } |
| 119 | + |
| 120 | + return nil |
| 121 | +} |
| 122 | + |
| 123 | +func (m *AtlasRepository) updateUserPasswordAndRoles(ctx context.Context, database string, username string, password string, roles Roles) error { |
| 124 | + user := &mongodbatlas.DatabaseUser{ |
| 125 | + Username: username, |
| 126 | + Password: password, |
| 127 | + Roles: m.getRoles(database, roles), |
| 128 | + } |
| 129 | + |
| 130 | + _, _, err := m.atlas.DatabaseUsers.Update(ctx, m.groupId, username, user) |
| 131 | + if err != nil { |
| 132 | + return err |
| 133 | + } |
| 134 | + |
| 135 | + return nil |
| 136 | +} |
0 commit comments