1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
14 "git.arvados.org/arvados.git/lib/controller/railsproxy"
15 "git.arvados.org/arvados.git/lib/controller/rpc"
16 "git.arvados.org/arvados.git/sdk/go/arvados"
17 "git.arvados.org/arvados.git/sdk/go/ctxlog"
18 "github.com/fsnotify/fsnotify"
19 "github.com/sirupsen/logrus"
22 type railsProxy = rpc.Conn
25 cluster *arvados.Cluster
26 *railsProxy // handles API methods that aren't defined on Conn itself
27 vocabularyCache *arvados.Vocabulary
32 func NewConn(cluster *arvados.Cluster) *Conn {
33 railsProxy := railsproxy.NewConn(cluster)
34 railsProxy.RedactHostInErrors = true
37 railsProxy: railsProxy,
39 conn.loginController = chooseLoginController(cluster, &conn)
43 func (conn *Conn) checkProperties(ctx context.Context, properties interface{}) error {
44 if properties == nil {
47 var props map[string]interface{}
48 switch properties := properties.(type) {
50 err := json.Unmarshal([]byte(properties), &props)
54 case map[string]interface{}:
57 return fmt.Errorf("unexpected properties type %T", properties)
59 voc, err := conn.VocabularyGet(ctx)
63 return voc.Check(props)
66 func watchVocabulary(logger logrus.FieldLogger, vocPath string, fn func()) {
67 watcher, err := fsnotify.NewWatcher()
69 logger.WithError(err).Error("vocabulary fsnotify setup failed")
74 err = watcher.Add(vocPath)
76 logger.WithError(err).Error("vocabulary file watcher failed")
82 case err, ok := <-watcher.Errors:
86 logger.WithError(err).Warn("vocabulary file watcher error")
87 case _, ok := <-watcher.Events:
91 for len(watcher.Events) > 0 {
99 func (conn *Conn) loadVocabularyFile() error {
100 vf, err := os.ReadFile(conn.cluster.API.VocabularyPath)
102 return fmt.Errorf("couldn't read vocabulary file %q: %v", conn.cluster.API.VocabularyPath, err)
104 mk := make([]string, 0, len(conn.cluster.Collections.ManagedProperties))
105 for k := range conn.cluster.Collections.ManagedProperties {
108 voc, err := arvados.NewVocabulary(vf, mk)
110 return fmt.Errorf("while loading vocabulary file %q: %s", conn.cluster.API.VocabularyPath, err)
114 return fmt.Errorf("while validating vocabulary file %q: %s", conn.cluster.API.VocabularyPath, err)
116 conn.vocabularyCache = voc
120 // VocabularyGet refreshes the vocabulary cache if necessary and returns it.
121 func (conn *Conn) VocabularyGet(ctx context.Context) (arvados.Vocabulary, error) {
122 if conn.cluster.API.VocabularyPath == "" {
123 return arvados.Vocabulary{}, nil
125 logger := ctxlog.FromContext(ctx)
126 if conn.vocabularyCache == nil {
127 // Initial load of vocabulary file.
128 err := conn.loadVocabularyFile()
130 logger.WithError(err).Error("error loading vocabulary file")
131 return arvados.Vocabulary{}, err
133 go watchVocabulary(logger, conn.cluster.API.VocabularyPath, func() {
134 logger.Info("vocabulary file changed, it'll be reloaded next time it's needed")
135 conn.reloadVocabulary = true
137 } else if conn.reloadVocabulary {
138 // Requested reload of vocabulary file.
139 conn.reloadVocabulary = false
140 err := conn.loadVocabularyFile()
142 logger.WithError(err).Error("error reloading vocabulary file - ignoring")
144 logger.Info("vocabulary file reloaded successfully")
147 return *conn.vocabularyCache, nil
150 // Logout handles the logout of conn giving to the appropriate loginController
151 func (conn *Conn) Logout(ctx context.Context, opts arvados.LogoutOptions) (arvados.LogoutResponse, error) {
152 return conn.loginController.Logout(ctx, opts)
155 // Login handles the login of conn giving to the appropriate loginController
156 func (conn *Conn) Login(ctx context.Context, opts arvados.LoginOptions) (arvados.LoginResponse, error) {
157 return conn.loginController.Login(ctx, opts)
160 // UserAuthenticate handles the User Authentication of conn giving to the appropriate loginController
161 func (conn *Conn) UserAuthenticate(ctx context.Context, opts arvados.UserAuthenticateOptions) (arvados.APIClientAuthorization, error) {
162 return conn.loginController.UserAuthenticate(ctx, opts)
165 func (conn *Conn) GroupContents(ctx context.Context, options arvados.GroupContentsOptions) (arvados.ObjectList, error) {
166 // The requested UUID can be a user (virtual home project), which we just pass on to
168 if strings.Index(options.UUID, "-j7d0g-") != 5 {
169 return conn.railsProxy.GroupContents(ctx, options)
172 var resp arvados.ObjectList
174 // Get the group object
175 respGroup, err := conn.GroupGet(ctx, arvados.GetOptions{UUID: options.UUID})
180 // If the group has groupClass 'filter', apply the filters before getting the contents.
181 if respGroup.GroupClass == "filter" {
182 if filters, ok := respGroup.Properties["filters"].([]interface{}); ok {
183 for _, f := range filters {
184 // f is supposed to be a []string
185 tmp, ok2 := f.([]interface{})
186 if !ok2 || len(tmp) < 3 {
187 return resp, fmt.Errorf("filter unparsable: %T, %+v, original field: %T, %+v\n", tmp, tmp, f, f)
189 var filter arvados.Filter
190 if attr, ok2 := tmp[0].(string); ok2 {
193 return resp, fmt.Errorf("filter unparsable: attribute must be string: %T, %+v, filter: %T, %+v\n", tmp[0], tmp[0], f, f)
195 if operator, ok2 := tmp[1].(string); ok2 {
196 filter.Operator = operator
198 return resp, fmt.Errorf("filter unparsable: operator must be string: %T, %+v, filter: %T, %+v\n", tmp[1], tmp[1], f, f)
200 filter.Operand = tmp[2]
201 options.Filters = append(options.Filters, filter)
204 return resp, fmt.Errorf("filter unparsable: not an array\n")
206 // Use the generic /groups/contents endpoint for filter groups
210 return conn.railsProxy.GroupContents(ctx, options)