1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
16 "git.arvados.org/arvados.git/lib/controller/railsproxy"
17 "git.arvados.org/arvados.git/lib/controller/rpc"
18 "git.arvados.org/arvados.git/sdk/go/arvados"
19 "git.arvados.org/arvados.git/sdk/go/ctxlog"
20 "git.arvados.org/arvados.git/sdk/go/httpserver"
21 "github.com/sirupsen/logrus"
24 type railsProxy = rpc.Conn
27 cluster *arvados.Cluster
28 *railsProxy // handles API methods that aren't defined on Conn itself
29 vocabularyCache *arvados.Vocabulary
30 vocabularyFileModTime time.Time
31 lastVocabularyRefreshCheck time.Time
32 lastVocabularyError error
36 func NewConn(cluster *arvados.Cluster) *Conn {
37 railsProxy := railsproxy.NewConn(cluster)
38 railsProxy.RedactHostInErrors = true
41 railsProxy: railsProxy,
43 conn.loginController = chooseLoginController(cluster, &conn)
47 func (conn *Conn) checkProperties(ctx context.Context, properties interface{}) error {
48 if properties == nil {
51 var props map[string]interface{}
52 switch properties := properties.(type) {
54 err := json.Unmarshal([]byte(properties), &props)
58 case map[string]interface{}:
61 return fmt.Errorf("unexpected properties type %T", properties)
63 voc, err := conn.VocabularyGet(ctx)
67 err = voc.Check(props)
69 return httpErrorf(http.StatusBadRequest, voc.Check(props).Error())
74 func (conn *Conn) maybeRefreshVocabularyCache(logger logrus.FieldLogger) error {
75 if conn.lastVocabularyRefreshCheck.Add(time.Second).After(time.Now()) {
76 // Throttle the access to disk to at most once per second.
79 conn.lastVocabularyRefreshCheck = time.Now()
80 fi, err := os.Stat(conn.cluster.API.VocabularyPath)
82 err = fmt.Errorf("couldn't stat vocabulary file %q: %v", conn.cluster.API.VocabularyPath, err)
83 conn.lastVocabularyError = err
86 if fi.ModTime().After(conn.vocabularyFileModTime) {
87 err = conn.loadVocabularyFile()
89 conn.lastVocabularyError = err
92 conn.vocabularyFileModTime = fi.ModTime()
93 conn.lastVocabularyError = nil
94 logger.Info("vocabulary file reloaded successfully")
99 func (conn *Conn) loadVocabularyFile() error {
100 vf, err := os.ReadFile(conn.cluster.API.VocabularyPath)
102 return fmt.Errorf("while reading the vocabulary file: %v", 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)
112 conn.vocabularyCache = voc
116 // LastVocabularyError returns the last error encountered while loading the
118 // Implements health.Func
119 func (conn *Conn) LastVocabularyError() error {
120 conn.maybeRefreshVocabularyCache(ctxlog.FromContext(context.Background()))
121 return conn.lastVocabularyError
124 // VocabularyGet refreshes the vocabulary cache if necessary and returns it.
125 func (conn *Conn) VocabularyGet(ctx context.Context) (arvados.Vocabulary, error) {
126 if conn.cluster.API.VocabularyPath == "" {
127 return arvados.Vocabulary{
128 Tags: map[string]arvados.VocabularyTag{},
131 logger := ctxlog.FromContext(ctx)
132 if conn.vocabularyCache == nil {
133 // Initial load of vocabulary file.
134 err := conn.loadVocabularyFile()
136 logger.WithError(err).Error("error loading vocabulary file")
137 return arvados.Vocabulary{}, err
140 err := conn.maybeRefreshVocabularyCache(logger)
142 logger.WithError(err).Error("error reloading vocabulary file - ignoring")
144 return *conn.vocabularyCache, nil
147 // Logout handles the logout of conn giving to the appropriate loginController
148 func (conn *Conn) Logout(ctx context.Context, opts arvados.LogoutOptions) (arvados.LogoutResponse, error) {
149 return conn.loginController.Logout(ctx, opts)
152 // Login handles the login of conn giving to the appropriate loginController
153 func (conn *Conn) Login(ctx context.Context, opts arvados.LoginOptions) (arvados.LoginResponse, error) {
154 return conn.loginController.Login(ctx, opts)
157 // UserAuthenticate handles the User Authentication of conn giving to the appropriate loginController
158 func (conn *Conn) UserAuthenticate(ctx context.Context, opts arvados.UserAuthenticateOptions) (arvados.APIClientAuthorization, error) {
159 return conn.loginController.UserAuthenticate(ctx, opts)
162 func (conn *Conn) GroupContents(ctx context.Context, options arvados.GroupContentsOptions) (arvados.ObjectList, error) {
163 // The requested UUID can be a user (virtual home project), which we just pass on to
165 if strings.Index(options.UUID, "-j7d0g-") != 5 {
166 return conn.railsProxy.GroupContents(ctx, options)
169 var resp arvados.ObjectList
171 // Get the group object
172 respGroup, err := conn.GroupGet(ctx, arvados.GetOptions{UUID: options.UUID})
177 // If the group has groupClass 'filter', apply the filters before getting the contents.
178 if respGroup.GroupClass == "filter" {
179 if filters, ok := respGroup.Properties["filters"].([]interface{}); ok {
180 for _, f := range filters {
181 // f is supposed to be a []string
182 tmp, ok2 := f.([]interface{})
183 if !ok2 || len(tmp) < 3 {
184 return resp, fmt.Errorf("filter unparsable: %T, %+v, original field: %T, %+v\n", tmp, tmp, f, f)
186 var filter arvados.Filter
187 if attr, ok2 := tmp[0].(string); ok2 {
190 return resp, fmt.Errorf("filter unparsable: attribute must be string: %T, %+v, filter: %T, %+v\n", tmp[0], tmp[0], f, f)
192 if operator, ok2 := tmp[1].(string); ok2 {
193 filter.Operator = operator
195 return resp, fmt.Errorf("filter unparsable: operator must be string: %T, %+v, filter: %T, %+v\n", tmp[1], tmp[1], f, f)
197 filter.Operand = tmp[2]
198 options.Filters = append(options.Filters, filter)
201 return resp, fmt.Errorf("filter unparsable: not an array\n")
203 // Use the generic /groups/contents endpoint for filter groups
207 return conn.railsProxy.GroupContents(ctx, options)
210 func httpErrorf(code int, format string, args ...interface{}) error {
211 return httpserver.ErrorWithStatus(fmt.Errorf(format, args...), code)