|
| 1 | +// Copyright 2025 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package healthcare |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "fmt" |
| 20 | + "net/http" |
| 21 | + |
| 22 | + "github.com/goccy/go-yaml" |
| 23 | + "github.com/googleapis/genai-toolbox/internal/sources" |
| 24 | + "github.com/googleapis/genai-toolbox/internal/util" |
| 25 | + "go.opentelemetry.io/otel/trace" |
| 26 | + "golang.org/x/oauth2" |
| 27 | + "golang.org/x/oauth2/google" |
| 28 | + "google.golang.org/api/googleapi" |
| 29 | + "google.golang.org/api/healthcare/v1" |
| 30 | + "google.golang.org/api/option" |
| 31 | +) |
| 32 | + |
| 33 | +const SourceKind string = "healthcare" |
| 34 | + |
| 35 | +// validate interface |
| 36 | +var _ sources.SourceConfig = Config{} |
| 37 | + |
| 38 | +type HealthcareServiceCreator func(tokenString string) (*healthcare.Service, error) |
| 39 | + |
| 40 | +func init() { |
| 41 | + if !sources.Register(SourceKind, newConfig) { |
| 42 | + panic(fmt.Sprintf("source kind %q already registered", SourceKind)) |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +func newConfig(ctx context.Context, name string, decoder *yaml.Decoder) (sources.SourceConfig, error) { |
| 47 | + actual := Config{Name: name} |
| 48 | + if err := decoder.DecodeContext(ctx, &actual); err != nil { |
| 49 | + return nil, err |
| 50 | + } |
| 51 | + return actual, nil |
| 52 | +} |
| 53 | + |
| 54 | +type Config struct { |
| 55 | + // Healthcare configs |
| 56 | + Name string `yaml:"name" validate:"required"` |
| 57 | + Kind string `yaml:"kind" validate:"required"` |
| 58 | + Project string `yaml:"project" validate:"required"` |
| 59 | + Region string `yaml:"region" validate:"required"` |
| 60 | + Dataset string `yaml:"dataset" validate:"required"` |
| 61 | + AllowedFHIRStores []string `yaml:"allowedFhirStores"` |
| 62 | + AllowedDICOMStores []string `yaml:"allowedDicomStores"` |
| 63 | + UseClientOAuth bool `yaml:"useClientOAuth"` |
| 64 | +} |
| 65 | + |
| 66 | +func (c Config) SourceConfigKind() string { |
| 67 | + return SourceKind |
| 68 | +} |
| 69 | + |
| 70 | +func (c Config) Initialize(ctx context.Context, tracer trace.Tracer) (sources.Source, error) { |
| 71 | + var service *healthcare.Service |
| 72 | + var serviceCreator HealthcareServiceCreator |
| 73 | + var tokenSource oauth2.TokenSource |
| 74 | + |
| 75 | + svc, tok, err := initHealthcareConnection(ctx, tracer, c.Name) |
| 76 | + if err != nil { |
| 77 | + return nil, fmt.Errorf("error creating service from ADC: %w", err) |
| 78 | + } |
| 79 | + if c.UseClientOAuth { |
| 80 | + serviceCreator, err = newHealthcareServiceCreator(ctx, tracer, c.Name) |
| 81 | + if err != nil { |
| 82 | + return nil, fmt.Errorf("error constructing service creator: %w", err) |
| 83 | + } |
| 84 | + } else { |
| 85 | + service = svc |
| 86 | + tokenSource = tok |
| 87 | + } |
| 88 | + |
| 89 | + dsName := fmt.Sprintf("projects/%s/locations/%s/datasets/%s", c.Project, c.Region, c.Dataset) |
| 90 | + if _, err = svc.Projects.Locations.Datasets.FhirStores.Get(dsName).Do(); err != nil { |
| 91 | + if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == http.StatusNotFound { |
| 92 | + return nil, fmt.Errorf("dataset '%s' not found", dsName) |
| 93 | + } |
| 94 | + return nil, fmt.Errorf("failed to verify existence of dataset '%s': %w", dsName, err) |
| 95 | + } |
| 96 | + |
| 97 | + allowedFHIRStores := make(map[string]struct{}) |
| 98 | + for _, store := range c.AllowedFHIRStores { |
| 99 | + name := fmt.Sprintf("%s/fhirStores/%s", dsName, store) |
| 100 | + _, err := svc.Projects.Locations.Datasets.FhirStores.Get(name).Do() |
| 101 | + if err != nil { |
| 102 | + if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == http.StatusNotFound { |
| 103 | + return nil, fmt.Errorf("allowedFhirStore '%s' not found in dataset '%s'", store, dsName) |
| 104 | + } |
| 105 | + return nil, fmt.Errorf("failed to verify allowedFhirStore '%s' in datasest '%s': %w", store, dsName, err) |
| 106 | + } |
| 107 | + allowedFHIRStores[store] = struct{}{} |
| 108 | + } |
| 109 | + allowedDICOMStores := make(map[string]struct{}) |
| 110 | + for _, store := range c.AllowedDICOMStores { |
| 111 | + name := fmt.Sprintf("%s/dicomStores/%s", dsName, store) |
| 112 | + _, err := svc.Projects.Locations.Datasets.DicomStores.Get(name).Do() |
| 113 | + if err != nil { |
| 114 | + if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == http.StatusNotFound { |
| 115 | + return nil, fmt.Errorf("allowedDicomStore '%s' not found in dataset '%s'", store, dsName) |
| 116 | + } |
| 117 | + return nil, fmt.Errorf("failed to verify allowedDicomFhirStore '%s' in datasest '%s': %w", store, dsName, err) |
| 118 | + } |
| 119 | + allowedDICOMStores[store] = struct{}{} |
| 120 | + } |
| 121 | + s := &Source{ |
| 122 | + name: c.Name, |
| 123 | + kind: SourceKind, |
| 124 | + project: c.Project, |
| 125 | + region: c.Region, |
| 126 | + dataset: c.Dataset, |
| 127 | + service: service, |
| 128 | + serviceCreator: serviceCreator, |
| 129 | + tokenSource: tokenSource, |
| 130 | + allowedFHIRStores: allowedFHIRStores, |
| 131 | + allowedDICOMStores: allowedDICOMStores, |
| 132 | + useClientOAuth: c.UseClientOAuth, |
| 133 | + } |
| 134 | + return s, nil |
| 135 | +} |
| 136 | + |
| 137 | +func newHealthcareServiceCreator(ctx context.Context, tracer trace.Tracer, name string) (func(string) (*healthcare.Service, error), error) { |
| 138 | + userAgent, err := util.UserAgentFromContext(ctx) |
| 139 | + if err != nil { |
| 140 | + return nil, err |
| 141 | + } |
| 142 | + return func(tokenString string) (*healthcare.Service, error) { |
| 143 | + return initHealthcareConnectionWithOAuthToken(ctx, tracer, name, userAgent, tokenString) |
| 144 | + }, nil |
| 145 | +} |
| 146 | + |
| 147 | +func initHealthcareConnectionWithOAuthToken(ctx context.Context, tracer trace.Tracer, name string, userAgent string, tokenString string) (*healthcare.Service, error) { |
| 148 | + ctx, span := sources.InitConnectionSpan(ctx, tracer, SourceKind, name) |
| 149 | + defer span.End() |
| 150 | + // Construct token source |
| 151 | + token := &oauth2.Token{ |
| 152 | + AccessToken: string(tokenString), |
| 153 | + } |
| 154 | + ts := oauth2.StaticTokenSource(token) |
| 155 | + |
| 156 | + // Initialize the Healthcare service with tokenSource |
| 157 | + service, err := healthcare.NewService(ctx, option.WithUserAgent(userAgent), option.WithTokenSource(ts)) |
| 158 | + if err != nil { |
| 159 | + return nil, fmt.Errorf("failed to create Healthcare service: %w", err) |
| 160 | + } |
| 161 | + return service, nil |
| 162 | +} |
| 163 | + |
| 164 | +func initHealthcareConnection(ctx context.Context, tracer trace.Tracer, name string) (*healthcare.Service, oauth2.TokenSource, error) { |
| 165 | + ctx, span := sources.InitConnectionSpan(ctx, tracer, SourceKind, name) |
| 166 | + defer span.End() |
| 167 | + |
| 168 | + cred, err := google.FindDefaultCredentials(ctx, healthcare.CloudHealthcareScope) |
| 169 | + if err != nil { |
| 170 | + return nil, nil, fmt.Errorf("failed to find default Google Cloud credentials with scope %q: %w", healthcare.CloudHealthcareScope, err) |
| 171 | + } |
| 172 | + |
| 173 | + userAgent, err := util.UserAgentFromContext(ctx) |
| 174 | + if err != nil { |
| 175 | + return nil, nil, err |
| 176 | + } |
| 177 | + |
| 178 | + service, err := healthcare.NewService(ctx, option.WithUserAgent(userAgent), option.WithCredentials(cred)) |
| 179 | + if err != nil { |
| 180 | + return nil, nil, fmt.Errorf("failed to create Healthcare service: %w", err) |
| 181 | + } |
| 182 | + return service, cred.TokenSource, nil |
| 183 | +} |
| 184 | + |
| 185 | +var _ sources.Source = &Source{} |
| 186 | + |
| 187 | +type Source struct { |
| 188 | + name string `yaml:"name"` |
| 189 | + kind string `yaml:"kind"` |
| 190 | + project string |
| 191 | + region string |
| 192 | + dataset string |
| 193 | + service *healthcare.Service |
| 194 | + serviceCreator HealthcareServiceCreator |
| 195 | + tokenSource oauth2.TokenSource |
| 196 | + allowedFHIRStores map[string]struct{} |
| 197 | + allowedDICOMStores map[string]struct{} |
| 198 | + useClientOAuth bool |
| 199 | +} |
| 200 | + |
| 201 | +func (s *Source) SourceKind() string { |
| 202 | + return SourceKind |
| 203 | +} |
| 204 | + |
| 205 | +func (s *Source) Project() string { |
| 206 | + return s.project |
| 207 | +} |
| 208 | + |
| 209 | +func (s *Source) Region() string { |
| 210 | + return s.region |
| 211 | +} |
| 212 | + |
| 213 | +func (s *Source) DatasetID() string { |
| 214 | + return s.dataset |
| 215 | +} |
| 216 | + |
| 217 | +func (s *Source) Service() *healthcare.Service { |
| 218 | + return s.service |
| 219 | +} |
| 220 | + |
| 221 | +func (s *Source) ServiceCreator() HealthcareServiceCreator { |
| 222 | + return s.serviceCreator |
| 223 | +} |
| 224 | + |
| 225 | +func (s *Source) TokenSource() oauth2.TokenSource { |
| 226 | + return s.tokenSource |
| 227 | +} |
| 228 | + |
| 229 | +func (s *Source) AllowedFHIRStores() map[string]struct{} { |
| 230 | + if len(s.allowedFHIRStores) == 0 { |
| 231 | + return nil |
| 232 | + } |
| 233 | + return s.allowedFHIRStores |
| 234 | +} |
| 235 | + |
| 236 | +func (s *Source) AllowedDICOMStores() map[string]struct{} { |
| 237 | + if len(s.allowedDICOMStores) == 0 { |
| 238 | + return nil |
| 239 | + } |
| 240 | + return s.allowedDICOMStores |
| 241 | +} |
| 242 | + |
| 243 | +func (s *Source) IsFHIRStoreAllowed(storeID string) bool { |
| 244 | + if len(s.allowedFHIRStores) == 0 { |
| 245 | + return true |
| 246 | + } |
| 247 | + _, ok := s.allowedFHIRStores[storeID] |
| 248 | + return ok |
| 249 | +} |
| 250 | + |
| 251 | +func (s *Source) IsDICOMStoreAllowed(storeID string) bool { |
| 252 | + if len(s.allowedDICOMStores) == 0 { |
| 253 | + return true |
| 254 | + } |
| 255 | + _, ok := s.allowedDICOMStores[storeID] |
| 256 | + return ok |
| 257 | +} |
| 258 | + |
| 259 | +func (s *Source) UseClientAuthorization() bool { |
| 260 | + return s.useClientOAuth |
| 261 | +} |
0 commit comments