@@ -20,9 +20,11 @@ import java.net.InetAddress
20
20
21
21
import akka .http .scaladsl .Http
22
22
import akka .http .scaladsl .model ._
23
+ import akka .http .scaladsl .model .headers .RawHeader
23
24
import akka .http .scaladsl .unmarshalling .Unmarshal
24
25
import akka .util .ByteString
25
26
import de .upb .cs .swt .delphi .instancemanagement .InstanceEnums .{ComponentType , InstanceState }
27
+ import de .upb .cs .swt .delphi .webapi .authorization .AuthProvider
26
28
import de .upb .cs .swt .delphi .webapi .{AppLogging , Configuration , _ }
27
29
import spray .json ._
28
30
@@ -92,7 +94,10 @@ object InstanceRegistry extends InstanceJsonSupport with AppLogging {
92
94
method = HttpMethods .POST ,
93
95
configuration.instanceRegistryUri + ReportOperationType .toOperationUriString(operationType, id))
94
96
95
- Await .result(Http (system).singleRequest(request) map { response =>
97
+ val useGenericNameForToken = operationType == ReportOperationType .Start // Must use generic name for startup, no id known at that point
98
+
99
+ Await .result(Http (system).singleRequest(request.withHeaders(RawHeader (" Authorization" ,
100
+ s " Bearer ${AuthProvider .generateJwt(useGenericName = useGenericNameForToken)}" ))) map { response =>
96
101
if (response.status == StatusCodes .OK ) {
97
102
log.info(s " Successfully reported ${operationType.toString} to Instance Registry. " )
98
103
Success ()
@@ -146,7 +151,7 @@ object InstanceRegistry extends InstanceJsonSupport with AppLogging {
146
151
configuration.instanceRegistryUri +
147
152
s " /matchingInstance?Id= ${configuration.assignedID.getOrElse(- 1 )}&ComponentType=ElasticSearch " )
148
153
149
- Await .result(Http (system).singleRequest(request) map { response =>
154
+ Await .result(Http (system).singleRequest(request.withHeaders( RawHeader ( " Authorization " , s " Bearer ${ AuthProvider .generateJwt()} " )) ) map { response =>
150
155
response.status match {
151
156
case StatusCodes .OK =>
152
157
try {
@@ -189,7 +194,7 @@ object InstanceRegistry extends InstanceJsonSupport with AppLogging {
189
194
configuration.instanceRegistryUri +
190
195
s " /matchingResult?CallerId= ${configuration.assignedID.getOrElse(- 1 )}&MatchedInstanceId= $idToPost&MatchingSuccessful= $isElasticSearchReachable" )
191
196
192
- Await .result(Http (system).singleRequest(request) map { response =>
197
+ Await .result(Http (system).singleRequest(request.withHeaders( RawHeader ( " Authorization " , s " Bearer ${ AuthProvider .generateJwt()} " )) ) map { response =>
193
198
if (response.status == StatusCodes .OK ) {
194
199
log.info(s " Successfully posted matching result to Instance Registry. " )
195
200
Success ()
@@ -216,7 +221,7 @@ object InstanceRegistry extends InstanceJsonSupport with AppLogging {
216
221
217
222
val request = HttpRequest (method = HttpMethods .POST , configuration.instanceRegistryUri + s " /deregister?Id= $id" )
218
223
219
- Await .result(Http (system).singleRequest(request) map { response =>
224
+ Await .result(Http (system).singleRequest(request.withHeaders( RawHeader ( " Authorization " , s " Bearer ${ AuthProvider .generateJwt()} " )) ) map { response =>
220
225
if (response.status == StatusCodes .OK ) {
221
226
log.info(" Successfully deregistered from Instance Registry." )
222
227
Success ()
@@ -237,7 +242,8 @@ object InstanceRegistry extends InstanceJsonSupport with AppLogging {
237
242
def postInstance (instance : Instance , uri : String )(): Future [HttpResponse ] = {
238
243
try {
239
244
val request = HttpRequest (method = HttpMethods .POST , uri = uri, entity = instance.toJson(instanceFormat).toString())
240
- Http (system).singleRequest(request)
245
+ // Use generic name for startup, no id present at this point
246
+ Http (system).singleRequest(request.withHeaders(RawHeader (" Authorization" ,s " Bearer ${AuthProvider .generateJwt(useGenericName = true )}" )))
241
247
} catch {
242
248
case dx : DeserializationException =>
243
249
log.warning(s " Failed to deregister to Instance Registry, exception: $dx" )
0 commit comments