@@ -109,6 +109,12 @@ public HttpTransport create() {
109109 // Maybe not on App Engine
110110 }
111111 }
112+ // Consider Compute
113+ try {
114+ return AuthCredentials .getComputeCredential ().getTransport ();
115+ } catch (Exception e ) {
116+ // Maybe not on GCE
117+ }
112118 return new NetHttpTransport ();
113119 }
114120 }
@@ -336,7 +342,7 @@ protected boolean projectIdRequired() {
336342 }
337343
338344 private static AuthCredentials defaultAuthCredentials () {
339- // Consider App Engine.
345+ // Consider App Engine. This will not be needed once issue #21 is fixed.
340346 if (appEngineAppId () != null ) {
341347 try {
342348 return AuthCredentials .createForAppEngine ();
@@ -348,8 +354,16 @@ private static AuthCredentials defaultAuthCredentials() {
348354 try {
349355 return AuthCredentials .createApplicationDefaults ();
350356 } catch (Exception ex ) {
351- return AuthCredentials . noCredentials ();
357+ // fallback to old-style
352358 }
359+
360+ // Consider old-style Compute. This will not be needed once issue #21 is fixed.
361+ try {
362+ return AuthCredentials .createForComputeEngine ();
363+ } catch (Exception ignore ) {
364+ // Maybe not on GCE
365+ }
366+ return AuthCredentials .noCredentials ();
353367 }
354368
355369 protected static String appEngineAppId () {
@@ -369,6 +383,19 @@ protected String defaultProject() {
369383 }
370384
371385 protected static String googleCloudProjectId () {
386+ try {
387+ URL url = new URL ("http://metadata/computeMetadata/v1/project/project-id" );
388+ HttpURLConnection connection = (HttpURLConnection ) url .openConnection ();
389+ connection .setRequestProperty ("X-Google-Metadata-Request" , "True" );
390+ InputStream input = connection .getInputStream ();
391+ if (connection .getResponseCode () == 200 ) {
392+ try (BufferedReader reader = new BufferedReader (new InputStreamReader (input , UTF_8 ))) {
393+ return reader .readLine ();
394+ }
395+ }
396+ } catch (IOException ignore ) {
397+ // ignore
398+ }
372399 File configDir ;
373400 if (System .getenv ().containsKey ("CLOUDSDK_CONFIG" )) {
374401 configDir = new File (System .getenv ("CLOUDSDK_CONFIG" ));
@@ -377,52 +404,38 @@ protected static String googleCloudProjectId() {
377404 } else {
378405 configDir = new File (System .getProperty ("user.home" ), ".config/gcloud" );
379406 }
380- FileReader fileReader = null ;
407+ FileReader fileReader ;
381408 try {
382409 fileReader = new FileReader (new File (configDir , "configurations/config_default" ));
383410 } catch (FileNotFoundException newConfigFileNotFoundEx ) {
384411 try {
385412 fileReader = new FileReader (new File (configDir , "properties" ));
386413 } catch (FileNotFoundException oldConfigFileNotFoundEx ) {
387- // ignore
414+ // return null if we can't find config file
415+ return null ;
388416 }
389417 }
390- if (fileReader != null ) {
391- try (BufferedReader reader = new BufferedReader (fileReader )) {
392- String line ;
393- String section = null ;
394- Pattern projectPattern = Pattern .compile ("^project\\ s*=\\ s*(.*)$" );
395- Pattern sectionPattern = Pattern .compile ("^\\ [(.*)\\ ]$" );
396- while ((line = reader .readLine ()) != null ) {
397- if (line .isEmpty () || line .startsWith (";" )) {
398- continue ;
399- }
400- line = line .trim ();
401- Matcher matcher = sectionPattern .matcher (line );
418+ try (BufferedReader reader = new BufferedReader (fileReader )) {
419+ String line ;
420+ String section = null ;
421+ Pattern projectPattern = Pattern .compile ("^project\\ s*=\\ s*(.*)$" );
422+ Pattern sectionPattern = Pattern .compile ("^\\ [(.*)\\ ]$" );
423+ while ((line = reader .readLine ()) != null ) {
424+ if (line .isEmpty () || line .startsWith (";" )) {
425+ continue ;
426+ }
427+ line = line .trim ();
428+ Matcher matcher = sectionPattern .matcher (line );
429+ if (matcher .matches ()) {
430+ section = matcher .group (1 );
431+ } else if (section == null || section .equals ("core" )) {
432+ matcher = projectPattern .matcher (line );
402433 if (matcher .matches ()) {
403- section = matcher .group (1 );
404- } else if (section == null || section .equals ("core" )) {
405- matcher = projectPattern .matcher (line );
406- if (matcher .matches ()) {
407- return matcher .group (1 );
408- }
434+ return matcher .group (1 );
409435 }
410436 }
411- } catch (IOException ex ) {
412- // ignore
413- }
414- }
415- try {
416- URL url = new URL ("http://metadata/computeMetadata/v1/project/project-id" );
417- HttpURLConnection connection = (HttpURLConnection ) url .openConnection ();
418- connection .setRequestProperty ("X-Google-Metadata-Request" , "True" );
419- InputStream input = connection .getInputStream ();
420- if (connection .getResponseCode () == 200 ) {
421- try (BufferedReader reader = new BufferedReader (new InputStreamReader (input , UTF_8 ))) {
422- return reader .readLine ();
423- }
424437 }
425- } catch (IOException ignore ) {
438+ } catch (IOException ex ) {
426439 // ignore
427440 }
428441 // return null if can't determine
0 commit comments