@@ -6,60 +6,50 @@ package apiv1
6
6
7
7
import (
8
8
"context"
9
+ "fmt"
9
10
11
+ connect "github.com/bufbuild/connect-go"
10
12
protocol "github.com/gitpod-io/gitpod/gitpod-protocol"
13
+ "github.com/gitpod-io/gitpod/public-api-server/pkg/auth"
11
14
"github.com/gitpod-io/gitpod/public-api-server/pkg/proxy"
12
15
v1 "github.com/gitpod-io/gitpod/public-api/v1"
16
+ "github.com/gitpod-io/gitpod/public-api/v1/v1connect"
13
17
"github.com/grpc-ecosystem/go-grpc-middleware/logging/logrus/ctxlogrus"
14
18
"github.com/relvacode/iso8601"
15
- "google.golang.org/grpc"
16
19
"google.golang.org/grpc/codes"
17
- "google.golang.org/grpc/metadata"
18
20
"google.golang.org/grpc/status"
19
21
"google.golang.org/protobuf/types/known/timestamppb"
20
22
)
21
23
22
24
func NewWorkspaceService (serverConnPool proxy.ServerConnectionPool ) * WorkspaceService {
23
25
return & WorkspaceService {
24
- connectionPool : serverConnPool ,
25
- UnimplementedWorkspacesServiceServer : & v1.UnimplementedWorkspacesServiceServer {},
26
+ connectionPool : serverConnPool ,
26
27
}
27
28
}
28
29
29
30
type WorkspaceService struct {
30
31
connectionPool proxy.ServerConnectionPool
31
32
32
- * v1. UnimplementedWorkspacesServiceServer
33
+ v1connect. UnimplementedWorkspacesServiceHandler
33
34
}
34
35
35
- func (w * WorkspaceService ) GetWorkspace (ctx context.Context , r * v1.GetWorkspaceRequest ) (* v1.GetWorkspaceResponse , error ) {
36
+ func (s * WorkspaceService ) GetWorkspace (ctx context.Context , req * connect.Request [v1.GetWorkspaceRequest ]) (* connect.Response [v1.GetWorkspaceResponse ], error ) {
37
+ token := auth .TokenFromContext (ctx )
36
38
logger := ctxlogrus .Extract (ctx )
37
- token , err := bearerTokenFromContext (ctx )
38
- if err != nil {
39
- return nil , err
40
- }
41
39
42
- server , err := w .connectionPool .Get (ctx , token )
40
+ server , err := s .connectionPool .Get (ctx , token )
43
41
if err != nil {
44
42
logger .WithError (err ).Error ("Failed to get connection to server." )
45
- return nil , status . Error ( codes . Internal , "failed to establish connection to downstream services" )
43
+ return nil , connect . NewError ( connect . CodeInternal , err )
46
44
}
47
45
48
- workspace , err := server .GetWorkspace (ctx , r .GetWorkspaceId ())
46
+ workspace , err := server .GetWorkspace (ctx , req . Msg .GetWorkspaceId ())
49
47
if err != nil {
50
48
logger .WithError (err ).Error ("Failed to get workspace." )
51
- converted := proxy .ConvertError (err )
52
- switch status .Code (converted ) {
53
- case codes .PermissionDenied :
54
- return nil , status .Error (codes .PermissionDenied , "insufficient permission to access workspace" )
55
- case codes .NotFound :
56
- return nil , status .Error (codes .NotFound , "workspace does not exist" )
57
- default :
58
- return nil , status .Error (codes .Internal , "unable to retrieve workspace" )
59
- }
49
+ return nil , proxy .ConvertError (err )
60
50
}
61
51
62
- return & v1.GetWorkspaceResponse {
52
+ return connect . NewResponse ( & v1.GetWorkspaceResponse {
63
53
Result : & v1.Workspace {
64
54
WorkspaceId : workspace .Workspace .ID ,
65
55
OwnerId : workspace .Workspace .OwnerID ,
@@ -73,72 +63,40 @@ func (w *WorkspaceService) GetWorkspace(ctx context.Context, r *v1.GetWorkspaceR
73
63
},
74
64
Description : workspace .Workspace .Description ,
75
65
},
76
- }, nil
66
+ }) , nil
77
67
}
78
68
79
- func (w * WorkspaceService ) GetOwnerToken (ctx context.Context , r * v1.GetOwnerTokenRequest ) (* v1.GetOwnerTokenResponse , error ) {
69
+ func (s * WorkspaceService ) GetOwnerToken (ctx context.Context , req * connect. Request [ v1.GetOwnerTokenRequest ] ) (* connect. Response [ v1.GetOwnerTokenResponse ] , error ) {
80
70
logger := ctxlogrus .Extract (ctx )
81
- token , err := bearerTokenFromContext (ctx )
82
- if err != nil {
83
- return nil , err
84
- }
71
+ token := auth .TokenFromContext (ctx )
85
72
86
- server , err := w .connectionPool .Get (ctx , token )
73
+ server , err := s .connectionPool .Get (ctx , token )
87
74
if err != nil {
88
75
logger .WithError (err ).Error ("Failed to get connection to server." )
89
76
return nil , status .Error (codes .Internal , "failed to establish connection to downstream services" )
90
77
}
91
78
92
- ownerToken , err := server .GetOwnerToken (ctx , r .GetWorkspaceId ())
79
+ ownerToken , err := server .GetOwnerToken (ctx , req . Msg .GetWorkspaceId ())
93
80
94
81
if err != nil {
95
82
logger .WithError (err ).Error ("Failed to get owner token." )
96
- converted := proxy .ConvertError (err )
97
- switch status .Code (converted ) {
98
- case codes .PermissionDenied :
99
- return nil , status .Error (codes .PermissionDenied , "insufficient permission to retrieve ownertoken" )
100
- case codes .NotFound :
101
- return nil , status .Error (codes .NotFound , "workspace does not exist" )
102
- default :
103
- return nil , status .Error (codes .Internal , "unable to retrieve owner token" )
104
- }
105
- }
106
-
107
- return & v1.GetOwnerTokenResponse {Token : ownerToken }, nil
108
- }
109
-
110
- func bearerTokenFromContext (ctx context.Context ) (string , error ) {
111
- md , ok := metadata .FromIncomingContext (ctx )
112
- if ! ok {
113
- return "" , status .Error (codes .Unauthenticated , "no credentials provided" )
114
- }
115
-
116
- values := md .Get ("authorization" )
117
- if len (values ) == 0 {
118
- return "" , status .Error (codes .Unauthenticated , "no authorization header specified" )
119
- }
120
- if len (values ) > 1 {
121
- return "" , status .Error (codes .Unauthenticated , "more than one authorization header specified, exactly one is required" )
83
+ return nil , proxy .ConvertError (err )
122
84
}
123
85
124
- token := values [0 ]
125
- return token , nil
86
+ return connect .NewResponse (& v1.GetOwnerTokenResponse {Token : ownerToken }), nil
126
87
}
127
88
128
- func (w * WorkspaceService ) ListWorkspaces (ctx context.Context , req * v1.ListWorkspacesRequest ) (* v1.ListWorkspacesResponse , error ) {
89
+ func (s * WorkspaceService ) ListWorkspaces (ctx context.Context , req * connect. Request [ v1.ListWorkspacesRequest ] ) (* connect. Response [ v1.ListWorkspacesResponse ] , error ) {
129
90
logger := ctxlogrus .Extract (ctx )
130
- token , err := bearerTokenFromContext (ctx )
131
- if err != nil {
132
- return nil , err
133
- }
91
+ token := auth .TokenFromContext (ctx )
134
92
135
- server , err := w .connectionPool .Get (ctx , token )
93
+ server , err := s .connectionPool .Get (ctx , token )
136
94
if err != nil {
137
95
logger .WithError (err ).Error ("Failed to get connection to server." )
138
96
return nil , status .Error (codes .Internal , "failed to establish connection to downstream services" )
139
97
}
140
98
141
- limit , err := getLimitFromPagination (req .Pagination )
99
+ limit , err := getLimitFromPagination (req .Msg . GetPagination () )
142
100
if err != nil {
143
101
// getLimitFromPagination returns gRPC errors
144
102
return nil , err
@@ -161,9 +119,11 @@ func (w *WorkspaceService) ListWorkspaces(ctx context.Context, req *v1.ListWorks
161
119
res = append (res , workspaceAndInstance )
162
120
}
163
121
164
- return & v1.ListWorkspacesResponse {
165
- Result : res ,
166
- }, nil
122
+ return connect .NewResponse (
123
+ & v1.ListWorkspacesResponse {
124
+ Result : res ,
125
+ },
126
+ ), nil
167
127
}
168
128
169
129
func getLimitFromPagination (pagination * v1.Pagination ) (int , error ) {
@@ -179,7 +139,7 @@ func getLimitFromPagination(pagination *v1.Pagination) (int, error) {
179
139
return defaultLimit , nil
180
140
}
181
141
if pagination .PageSize < 0 || maxLimit < pagination .PageSize {
182
- return 0 , grpc . Errorf ( codes . InvalidArgument , "invalid pagination page size (must be 0 < x < %d)" , maxLimit )
142
+ return 0 , connect . NewError ( connect . CodeInvalidArgument , fmt . Errorf ( "invalid pagination page size (must be 0 < x < %d)" , maxLimit ) )
183
143
}
184
144
185
145
return int (pagination .PageSize ), nil
@@ -192,7 +152,7 @@ func convertWorkspaceInfo(input *protocol.WorkspaceInfo) (*v1.ListWorkspacesResp
192
152
creationTime , err := parseGitpodTimestamp (wsi .CreationTime )
193
153
if err != nil {
194
154
// TODO(cw): should this really return an error and possibly fail the entire operation?
195
- return nil , grpc . Errorf ( codes . FailedPrecondition , "cannot parse creation time: %v" , err )
155
+ return nil , connect . NewError ( connect . CodeFailedPrecondition , fmt . Errorf ( "cannot parse creation time: %v" , err ) )
196
156
}
197
157
198
158
var phase v1.WorkspaceInstanceStatus_Phase
@@ -219,7 +179,7 @@ func convertWorkspaceInfo(input *protocol.WorkspaceInfo) (*v1.ListWorkspacesResp
219
179
phase = v1 .WorkspaceInstanceStatus_PHASE_STOPPED
220
180
default :
221
181
// TODO(cw): should this really return an error and possibly fail the entire operation?
222
- return nil , grpc . Errorf ( codes . FailedPrecondition , "cannot convert instance phase: %s" , wsi .Status .Phase )
182
+ return nil , connect . NewError ( connect . CodeFailedPrecondition , fmt . Errorf ( "cannot convert instance phase: %s" , wsi .Status .Phase ) )
223
183
}
224
184
225
185
var admissionLevel v1.AdmissionLevel
0 commit comments